高级 API 索引

本页列出了有启用 async/await 的所有高级 asyncio API。

任务

要运行 asyncio 程序、创建任务,及采用超时等待多个事情的实用工具。

run() 创建事件循环,运行协程,关闭循环。
Runner 用于简化多个 async 函数调用的上下文管理器。
Task 任务对象。
TaskGroup A context manager that holds a group of tasks. Provides a convenient and reliable way to wait for all tasks in the group to finish.
create_task() Start an asyncio Task, then returns it.
current_task() Return the current Task.
all_tasks() Return all tasks that are not yet finished for an event loop.
await sleep() Sleep for a number of seconds.
await gather() Schedule and wait for things concurrently.
await wait_for() Run with a timeout.
await shield() Shield from cancellation.
await wait() Monitor for completion.
timeout() Run with a timeout. Useful in cases when wait_for is not suitable.
to_thread() Asynchronously run a function in a separate OS thread.
run_coroutine_threadsafe() Schedule a coroutine from another OS thread.
for in as_completed() Monitor for completion with a for 循环。

范例

队列

Queues should be used to distribute work amongst multiple asyncio Tasks, implement connection pools, and pub/sub patterns.

Queue A FIFO queue.
PriorityQueue A priority queue.
LifoQueue A LIFO queue.

范例

子进程

Utilities to spawn subprocesses and run shell commands.

await create_subprocess_exec() 创建子进程。
await create_subprocess_shell() Run a shell command.

范例

High-level APIs to work with network IO.

await open_connection() Establish a TCP connection.
await open_unix_connection() Establish a Unix socket connection.
await start_server() 启动 TCP 服务器。
await start_unix_server() 启动 Unix 套接字服务器。
StreamReader High-level async/await object to receive network data.
StreamWriter High-level async/await object to send network data.

范例

同步

Threading-like synchronization primitives that can be used in Tasks.

Lock A mutex lock.
Event An event object.
Condition A condition object.
Semaphore A semaphore.
BoundedSemaphore A bounded semaphore.
Barrier A barrier object.

范例

异常

asyncio.CancelledError Raised when a Task is cancelled. See also Task.cancel() .
asyncio.BrokenBarrierError Raised when a Barrier is broken. See also Barrier.wait() .

范例

内容表

上一话题

延伸

下一话题

低级 API 索引

本页