Python asyncio Module
Example
Run a simple coroutine using asyncio.run():
import asyncio
async def main():
print("hello")
await asyncio.sleep(0)
print("world")
asyncio.run(main())
Try it Yourself »
Definition and Usage
The asyncio
module provides an event loop, tasks, and I/O primitives for concurrent code.
Use async
/await
to write structured asynchronous programs, schedule coroutines, and work with networking, subprocesses, and synchronization primitives.
Members
Member | Description |
---|---|
all_tasks() | Return a set of all not-yet-finished tasks for the current event loop. |
as_completed() | Iterate results as soon as the awaited tasks complete. |
BoundedSemaphore | Like Semaphore , but prevents the counter from going above the initial value. |
CancelledError | Exception raised when a task is cancelled. |
Condition | Coordinate coroutines that need to wait for some shared state to change. |
create_subprocess_exec() | Spawn a subprocess (exec form) and interact with it asynchronously. |
create_subprocess_shell() | Spawn a subprocess using the system shell. |
create_task() | Schedule the execution of a coroutine object as a Task. |
current_task() | Return the currently running Task. |
Event | Synchronization primitive for coroutines to wait for a signal. |
gather() | Run multiple awaitables concurrently and collect results. |
get_event_loop() | Return the current event loop (deprecated in Python 3.10+; use get_running_loop()). |
get_running_loop() | Return the currently running event loop. |
LifoQueue | Stack (LIFO) queue for coroutine producers/consumers. |
Lock | Mutual exclusion lock for coroutines. |
new_event_loop() | Create and return a new event loop object. |
open_connection() | Open a TCP client connection. |
PriorityQueue | Queue where items are retrieved by priority (lower values first). |
Queue | FIFO queue for coroutine producers/consumers. |
run() | Run the main entry point of an asyncio program. |
run_coroutine_threadsafe() | Submit a coroutine to an event loop from a different thread. |
Semaphore | Counting semaphore for limiting concurrent access. |
set_event_loop() | Set the current event loop for the current thread. |
shield() | Protect an awaitable from cancellation (the outer wait can be cancelled). |
sleep() | Coroutine that completes after a given time. |
start_server() | Create a TCP server. |
StreamReader | Async stream reader for reading data from network connections. |
StreamWriter | Async stream writer for writing data to network connections. |
Task | Wrapper that schedules and runs a coroutine; await it to get the result. |
timeout() | Async context manager to apply a timeout to a block of await s. |
TimeoutError | Exception raised when an operation exceeds its timeout. |
to_thread() | Run a blocking function in a separate thread and await the result. |
wait() | Wait for a set of tasks to complete according to a condition (FIRST_COMPLETED/ALL_COMPLETED). |
wait_for() | Run an awaitable with a timeout and cancel it if it takes too long. |