asyncio
本页列出了有启用 async/await 的所有高级 asyncio API。
要运行 asyncio 程序、创建任务,及采用超时等待多个事情的实用工具。
run()
Runner
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()
current_task()
all_tasks()
await sleep()
await
sleep()
await gather()
gather()
await wait_for()
wait_for()
await shield()
shield()
await wait()
wait()
timeout()
Run with a timeout. Useful in cases when wait_for is not suitable.
wait_for
to_thread()
run_coroutine_threadsafe()
for in as_completed()
for in
as_completed()
Monitor for completion with a for 循环。
for
范例
Using asyncio.gather() to run things in parallel .
Using asyncio.wait_for() to enforce a timeout .
Cancellation .
使用 asyncio.sleep() .
See also the main Tasks documentation page .
Queues should be used to distribute work amongst multiple asyncio Tasks, implement connection pools, and pub/sub patterns.
Queue
PriorityQueue
LifoQueue
Using asyncio.Queue to distribute workload between several Tasks .
另请参阅 Queues documentation page .
Utilities to spawn subprocesses and run shell commands.
await create_subprocess_exec()
create_subprocess_exec()
await create_subprocess_shell()
create_subprocess_shell()
Executing a shell command .
另请参阅 subprocess APIs 文档编制。
High-level APIs to work with network IO.
await open_connection()
open_connection()
await open_unix_connection()
open_unix_connection()
await start_server()
start_server()
await start_unix_server()
start_unix_server()
StreamReader
StreamWriter
Example TCP client .
另请参阅 streams APIs 文档编制。
Threading-like synchronization primitives that can be used in Tasks.
Lock
Event
Condition
Semaphore
BoundedSemaphore
Barrier
使用 asyncio.Event .
使用 asyncio.Barrier .
另请参阅文档编制为 asyncio 同步原语 .
asyncio.CancelledError
Raised when a Task is cancelled. See also Task.cancel() .
Task.cancel()
asyncio.BrokenBarrierError
Raised when a Barrier is broken. See also Barrier.wait() .
Barrier.wait()
Handling CancelledError to run code on cancellation request .
See also the full list of asyncio-specific exceptions .
延伸
低级 API 索引
键入搜索术语或模块、类、函数名称。