内容表

  • 高级 API 索引
    • 任务
    • 队列
    • 子进程
    • 流
    • 同步
    • 就业培训     下载中心     Wiki     联络
      登录   注册

      Log
      1. 首页
      2. Python 3.12.4
      3. 索引
      4. 模块
      5. 下一
      6. 上一
      7. Python 标准库
      8. 网络和进程间通信
      9. asyncio — 异步 I/O
      10. 高级 API 索引

      高级 API 索引 ¶

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

      任务 ¶

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

      run()

      创建事件循环,运行协程,关闭循环。

      Runner

      用于简化多个 async 函数调用的上下文管理器。

      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()

      Start an asyncio Task, then returns it.

      current_task()

      Return the current Task.

      all_tasks()

      Return all tasks that are not yet finished for an event loop.

      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.

      timeout()

      Run with a timeout. Useful in cases when wait_for is not suitable.

      to_thread()

      Asynchronously run a function in a separate OS thread.

      run_coroutine_threadsafe()

      Schedule a coroutine from another OS thread.

      for in as_completed()

      Monitor for completion with a 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

      A FIFO queue.

      PriorityQueue

      A priority queue.

      LifoQueue

      A LIFO queue.

      范例

      • Using asyncio.Queue to distribute workload between several Tasks .

      • 另请参阅 Queues documentation page .

      子进程 ¶

      Utilities to spawn subprocesses and run shell commands.

      await create_subprocess_exec()

      创建子进程。

      await create_subprocess_shell()

      Run a shell command.

      范例

      • Executing 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.

      范例

      • Example TCP client .

      • 另请参阅 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.

      Barrier

      A barrier object.

      范例

      • 使用 asyncio.Event .

      • 使用 asyncio.Barrier .

      • 另请参阅文档编制为 asyncio 同步原语 .

      异常 ¶

      asyncio.CancelledError

      Raised when a Task is cancelled. See also Task.cancel() .

      asyncio.BrokenBarrierError

      Raised when a Barrier is broken. See also Barrier.wait() .

      范例

      • Handling CancelledError to run code on cancellation request .

      • See also the full list of asyncio-specific exceptions .

      内容表

      • 高级 API 索引
        • 任务
        • 队列
        • 子进程
        • 流
        • 同步
        • 异常

      上一话题

      延伸

      下一话题

      低级 API 索引

      本页

      • 报告 Bug
      • 展示源

      快速搜索

      键入搜索术语或模块、类、函数名称。

      1. 首页
      2. Python 3.12.4
      3. 索引
      4. 模块
      5. 下一
      6. 上一
      7. Python 标准库
      8. 网络和进程间通信
      9. asyncio — 异步 I/O
      10. 高级 API 索引

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1