Yury Selivanov | 02a0a19 | 2017-12-14 09:42:21 -0500 | [diff] [blame] | 1 | __all__ = 'run', |
| 2 | |
| 3 | from . import coroutines |
| 4 | from . import events |
| 5 | |
| 6 | |
| 7 | def run(main, *, debug=False): |
| 8 | """Run a coroutine. |
| 9 | |
| 10 | This function runs the passed coroutine, taking care of |
| 11 | managing the asyncio event loop and finalizing asynchronous |
| 12 | generators. |
| 13 | |
| 14 | This function cannot be called when another asyncio event loop is |
| 15 | running in the same thread. |
| 16 | |
| 17 | If debug is True, the event loop will be run in debug mode. |
| 18 | |
| 19 | This function always creates a new event loop and closes it at the end. |
| 20 | It should be used as a main entry point for asyncio programs, and should |
| 21 | ideally only be called once. |
| 22 | |
| 23 | Example: |
| 24 | |
| 25 | async def main(): |
| 26 | await asyncio.sleep(1) |
| 27 | print('hello') |
| 28 | |
| 29 | asyncio.run(main()) |
| 30 | """ |
| 31 | if events._get_running_loop() is not None: |
| 32 | raise RuntimeError( |
| 33 | "asyncio.run() cannot be called from a running event loop") |
| 34 | |
| 35 | if not coroutines.iscoroutine(main): |
| 36 | raise ValueError("a coroutine was expected, got {!r}".format(main)) |
| 37 | |
| 38 | loop = events.new_event_loop() |
| 39 | try: |
| 40 | events.set_event_loop(loop) |
| 41 | loop.set_debug(debug) |
| 42 | return loop.run_until_complete(main) |
| 43 | finally: |
| 44 | try: |
| 45 | loop.run_until_complete(loop.shutdown_asyncgens()) |
| 46 | finally: |
| 47 | events.set_event_loop(None) |
| 48 | loop.close() |