• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1__all__ = 'run',
2
3from . import coroutines
4from . import events
5from . import tasks
6
7
8def run(main, *, debug=False):
9    """Run a coroutine.
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)
42        loop.set_debug(debug)
43        return loop.run_until_complete(main)
44    finally:
45        try:
46            _cancel_all_tasks(loop)
47            loop.run_until_complete(loop.shutdown_asyncgens())
48        finally:
49            events.set_event_loop(None)
50            loop.close()
51
52
53def _cancel_all_tasks(loop):
54    to_cancel = tasks.all_tasks(loop)
55    if not to_cancel:
56        return
57
58    for task in to_cancel:
59        task.cancel()
60
61    loop.run_until_complete(
62        tasks.gather(*to_cancel, loop=loop, return_exceptions=True))
63
64    for task in to_cancel:
65        if task.cancelled():
66            continue
67        if task.exception() is not None:
68            loop.call_exception_handler({
69                'message': 'unhandled exception during asyncio.run() shutdown',
70                'exception': task.exception(),
71                'task': task,
72            })
73