Yury Selivanov | 02a0a19 | 2017-12-14 09:42:21 -0500 | [diff] [blame] | 1 | __all__ = 'run', |
| 2 | |
| 3 | from . import coroutines |
| 4 | from . import events |
Yury Selivanov | a4afcdf | 2018-01-21 14:56:59 -0500 | [diff] [blame] | 5 | from . import tasks |
Yury Selivanov | 02a0a19 | 2017-12-14 09:42:21 -0500 | [diff] [blame] | 6 | |
| 7 | |
Shantanu | 0770ad9 | 2020-09-02 21:54:46 -0700 | [diff] [blame] | 8 | def run(main, *, debug=None): |
Kyle Stanley | e407013 | 2019-09-30 20:12:21 -0400 | [diff] [blame] | 9 | """Execute the coroutine and return the result. |
Yury Selivanov | 02a0a19 | 2017-12-14 09:42:21 -0500 | [diff] [blame] | 10 | |
| 11 | This function runs the passed coroutine, taking care of |
| 12 | managing the asyncio event loop and finalizing asynchronous |
| 13 | generators. |
| 14 | |
| 15 | This function cannot be called when another asyncio event loop is |
| 16 | running in the same thread. |
| 17 | |
| 18 | If debug is True, the event loop will be run in debug mode. |
| 19 | |
| 20 | This function always creates a new event loop and closes it at the end. |
| 21 | It should be used as a main entry point for asyncio programs, and should |
| 22 | ideally only be called once. |
| 23 | |
| 24 | Example: |
| 25 | |
| 26 | async def main(): |
| 27 | await asyncio.sleep(1) |
| 28 | print('hello') |
| 29 | |
| 30 | asyncio.run(main()) |
| 31 | """ |
| 32 | if events._get_running_loop() is not None: |
| 33 | raise RuntimeError( |
| 34 | "asyncio.run() cannot be called from a running event loop") |
| 35 | |
| 36 | if not coroutines.iscoroutine(main): |
| 37 | raise ValueError("a coroutine was expected, got {!r}".format(main)) |
| 38 | |
| 39 | loop = events.new_event_loop() |
| 40 | try: |
| 41 | events.set_event_loop(loop) |
Shantanu | 0770ad9 | 2020-09-02 21:54:46 -0700 | [diff] [blame] | 42 | if debug is not None: |
| 43 | loop.set_debug(debug) |
Yury Selivanov | 02a0a19 | 2017-12-14 09:42:21 -0500 | [diff] [blame] | 44 | return loop.run_until_complete(main) |
| 45 | finally: |
| 46 | try: |
Yury Selivanov | a4afcdf | 2018-01-21 14:56:59 -0500 | [diff] [blame] | 47 | _cancel_all_tasks(loop) |
Yury Selivanov | 02a0a19 | 2017-12-14 09:42:21 -0500 | [diff] [blame] | 48 | loop.run_until_complete(loop.shutdown_asyncgens()) |
Kyle Stanley | 9fdc64c | 2019-09-19 08:47:22 -0400 | [diff] [blame] | 49 | loop.run_until_complete(loop.shutdown_default_executor()) |
Yury Selivanov | 02a0a19 | 2017-12-14 09:42:21 -0500 | [diff] [blame] | 50 | finally: |
| 51 | events.set_event_loop(None) |
| 52 | loop.close() |
Yury Selivanov | a4afcdf | 2018-01-21 14:56:59 -0500 | [diff] [blame] | 53 | |
| 54 | |
| 55 | def _cancel_all_tasks(loop): |
Yury Selivanov | 416c1eb | 2018-05-28 17:54:02 -0400 | [diff] [blame] | 56 | to_cancel = tasks.all_tasks(loop) |
Yury Selivanov | a4afcdf | 2018-01-21 14:56:59 -0500 | [diff] [blame] | 57 | if not to_cancel: |
| 58 | return |
| 59 | |
| 60 | for task in to_cancel: |
| 61 | task.cancel() |
| 62 | |
Yurii Karabas | e4fe303 | 2020-11-28 10:21:17 +0200 | [diff] [blame] | 63 | loop.run_until_complete(tasks.gather(*to_cancel, return_exceptions=True)) |
Yury Selivanov | a4afcdf | 2018-01-21 14:56:59 -0500 | [diff] [blame] | 64 | |
| 65 | for task in to_cancel: |
| 66 | if task.cancelled(): |
| 67 | continue |
| 68 | if task.exception() is not None: |
| 69 | loop.call_exception_handler({ |
| 70 | 'message': 'unhandled exception during asyncio.run() shutdown', |
| 71 | 'exception': task.exception(), |
| 72 | 'task': task, |
| 73 | }) |