高级 API 索引

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

任务

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

run() 创建事件循环,运行协程,关闭循环。
create_task() Start an asyncio Task.
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.
current_task() Return the current Task.
all_tasks() Return all tasks for an event loop.
Task 任务对象。
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.

范例

异常

asyncio.TimeoutError Raised on timeout by functions like wait_for() . Keep in mind that asyncio.TimeoutError is unrelated to the built-in TimeoutError 异常。
asyncio.CancelledError Raised when a Task is cancelled. See also Task.cancel() .

范例

内容表

上一话题

平台支持

下一话题

低级 API 索引

本页