高级 API 索引

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

Tasks

Util,用于运行 asyncio 程序,创建 Tasks 并 await 多个事件超时。

run()创建事件循环,运行协程,关闭循环。
create_task()启动异步任务。
await sleep()睡眠几秒钟。
await gather()安排并 await 并发。
await wait_for()运行超时。
await shield()屏蔽取消。
await wait()监控完成情况。
current_task()返回当前任务。
all_tasks()返回事件循环的所有任务。
TaskTask object.
run_coroutine_threadsafe()从另一个 OS 线程安排协程。
for in as_completed()passfor循环监视完成情况。

Examples

Queues

队列应用于在多个异步任务之间分配工作,实现连接池和发布/订阅模式。

QueueFIFO 队列。
PriorityQueue优先级队列。
LifoQueueLIFO 队列。

Examples

Subprocesses

产生子流程和运行 shell 命令的 Util。

await create_subprocess_exec()创建一个子流程。
await create_subprocess_shell()运行一个 shell 命令。

Examples

Streams

与网络 IO 配合使用的高级 API。

await open_connection()构建 TCP 连接。
await open_unix_connection()构建 Unix 套接字连接。
await start_server()启动 TCP 服务器。
await start_unix_server()启动 Unix 套接字服务器。
StreamReader高级异步/await 对象,用于接收网络数据。
StreamWriter高级异步/await 对象以发送网络数据。

Examples

Synchronization

可在 Tasks 中使用的类似线程的同步 Primitives。

Lock互斥锁。
Event事件对象。
Condition条件对象。
SemaphoreA semaphore.
BoundedSemaphore有界 signal 量。

Examples

Exceptions

asyncio.TimeoutError在超时时由wait_for()之类的函数引发。请记住,asyncio.TimeoutError与内置TimeoutError异常“无关”。
asyncio.CancelledError取消任务时引发。另请参见Task.cancel()

Examples