On this page
asyncio.run
Prev: asyncio.print_call_graph | Up: asyncio | Next: asyncio.run_coroutine_threadsafe
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.as_completed
Ref: docs.python.org — asyncio.run
Tags: #public #python3 #stdlib #asyncio
Import
import asyncio
from asyncio import run
Signature & Overview
asyncio.run(main, *, debug=None, loop_factory=None)
Execute the coroutine and return the result.
Usage
import asyncio
async def main() -> int:
await asyncio.sleep(0)
return 42
assert asyncio.run(main()) == 42
Gotchas
- Top-level entry point: Creates a fresh loop, runs the coroutine, cancels remaining tasks, shuts down async generators, and closes the loop.
- Cannot be called inside an already-running loop: Calling
asyncio.run()from inside anasync def(or Jupyter cell) raisesRuntimeError.
REPL Inspect
from asyncio import run
help(run)
Linked from
Related notes
- tuts/python3/stdlib_module/asyncio/print_call_graph Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/run_coroutine_threadsafe Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractEventLoop Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractServer Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/all_tasks Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/as_completed Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/Barrier shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseEventLoop shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseProtocol shared tags: #asyncio, #stdlib