On this page
高级 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() | 返回事件循环的所有任务。 |
Task | Task object. |
run_coroutine_threadsafe() | 从另一个 OS 线程安排协程。 |
for in as_completed() |
passfor 循环监视完成情况。 |
Examples
另请参见主要的任务文档页面。
Queues
队列应用于在多个异步任务之间分配工作,实现连接池和发布/订阅模式。
Queue | FIFO 队列。 |
PriorityQueue | 优先级队列。 |
LifoQueue | LIFO 队列。 |
Examples
Subprocesses
产生子流程和运行 shell 命令的 Util。
await create_subprocess_exec() |
创建一个子流程。 |
await create_subprocess_shell() |
运行一个 shell 命令。 |
Examples
另请参见subprocess APIs文档。
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
另请参见streams APIs文档。
Synchronization
可在 Tasks 中使用的类似线程的同步 Primitives。
Lock | 互斥锁。 |
Event | 事件对象。 |
Condition | 条件对象。 |
Semaphore | A semaphore. |
BoundedSemaphore | 有界 signal 量。 |
Examples
另请参阅 asyncio synchronization primitives的文档。
Exceptions
asyncio.TimeoutError | 在超时时由wait_for()之类的函数引发。请记住,asyncio.TimeoutError 与内置TimeoutError异常“无关”。 |
asyncio.CancelledError | 取消任务时引发。另请参见Task.cancel()。 |
Examples