PyDocs

Nav

Networking and Interprocess Communication

run

asyncio

function · Added in 3.7 (source) not WASI
  • run(coro, *, debug=None, loop_factory=None)
Execute the coroutine and return the result.

This function runs the passed coroutine, taking care of
managing the asyncio event loop, finalizing asynchronous
generators and closing the default executor.

This function cannot be called when another asyncio event loop is
running in the same thread.

If debug is True, the event loop will be run in debug mode.
If loop_factory is passed, it is used for new event loop creation.

This function always creates a new event loop and closes it at the end.
It should be used as a main entry point for asyncio programs, and should
ideally only be called once.

The executor is given a timeout duration of 5 minutes to shutdown.
If the executor hasn't finished within that duration, a warning is
emitted and the executor is closed.

Example:

    async def main():
        await asyncio.sleep(1)
        print('hello')

    asyncio.run(main())
Changed in 3.9, 3.10, 3.12, 3.14
  • 3.9: Updated to use loop.shutdown_default_executor().
  • 3.10: debug is None by default to respect the global debug mode settings.
  • 3.12: Added loop_factory parameter.
  • 3.14: coro can be any awaitable object.
1 Example
>>> import asyncio

>>> async def main():
...     print('hello')
...     await asyncio.sleep(1)
...     print('world')

>>> asyncio.run(main())
hello
world
from the Python docs
See Also
No see-alsos for asyncio/run
0 Notes
No notes for run