本页列出了有启用 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
循环。
|
范例
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
|
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. |
范例
另请参阅 subprocess APIs 文档编制。
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. |
范例
另请参阅 streams APIs 文档编制。
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 同步原语 .
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()
.
|
范例
Handling CancelledError to run code on cancellation request .
See also the full list of asyncio-specific exceptions .