Python 3.7+Async programmingfunction
asyncio.run()
event loop
Runs a top-level coroutine in a new event loop. The main entry point of an async program (Python 3.7+).
Syntax
asyncio.run(coro)
You cannot call it from inside a loop that is already running — in Jupyter or within another coroutine you get RuntimeError: asyncio.run() cannot be called from a running event loop. Every call builds a fresh loop and closes it on exit, cancelling whatever is still pending, so loop-bound objects such as Queue, Lock or open connections cannot be shared across two calls. Rule of thumb: exactly one call, at the very top of your program.
Examples
import asyncio
async def main():
return 42
result = asyncio.run(main())
result # → 42
import asyncio
async def hello():
print('start')
await asyncio.sleep(0)
print('end')
asyncio.run(hello()) # → start / end
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy