内容表

  • 运行器
    • 运行异步程序
    • Runner 上下文管理器
    • 处理键盘中断

上一话题

就业培训     下载中心     Wiki     联络
登录   注册

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

运行器 ¶

源代码: Lib/asyncio/runners.py

此节提纲了运行 asyncio 代码的高级 asyncio 原语。

它们的构建是基于 事件循环 旨在简化常见广泛零散情景下的 async 代码使用。

  • 运行异步程序

  • Runner 上下文管理器

  • 处理键盘中断

运行异步程序 ¶

asyncio. run ( coro , * , debug = None , loop_factory = None ) ¶

执行 协程 coro 并返回结果。

此函数运行传递的协程,负责管理 asyncio 事件循环, 定稿异步生成器 , and closing the executor.

无法调用此函数,当另一 asyncio 事件循环在同一线程中运行时。

若 debug is True ,以调试模式运行事件循环。 False 明确禁用调试模式。 None 用于遵守全局 调试模式 设置。

若 loop_factory 不是 None , it is used to create a new event loop; otherwise asyncio.new_event_loop() is used. The loop is closed at the end. This function should be used as a main entry point for asyncio programs, and should ideally only be called once. It is recommended to use loop_factory to configure the event loop instead of policies.

The executor is given a timeout duration of 5 minutes to shutdown. If the executor hasn’t finished within that duration, a warning is emitted and the executor is closed.

范例:

async def main():
    await asyncio.sleep(1)
    print('hello')
asyncio.run(main())
													

Added in version 3.7.

3.9 版改变: 更新为使用 loop.shutdown_default_executor() .

3.10 版改变: debug is None 默认情况下,将遵守全局调试模式设置。

Changed in version 3.12: 添加 loop_factory 参数。

Runner 上下文管理器 ¶

class asyncio. Runner ( * , debug = None , loop_factory = None ) ¶

上下文管理器能简化 multiple async 函数调用在同一上下文中。

有时,应该调用几个顶层 async 函数在同一 事件循环 and contextvars.Context .

若 debug is True ,以调试模式运行事件循环。 False 明确禁用调试模式。 None 用于遵守全局 调试模式 设置。

loop_factory 可以用于覆盖循环创建。它负责 loop_factory 将创建的循环设为当前循环。默认情况下 asyncio.new_event_loop() 被使用并设为当前事件循环采用 asyncio.set_event_loop() if loop_factory is None .

基本上, asyncio.run() 范例可以按 Runner 用法重写:

async def main():
    await asyncio.sleep(1)
    print('hello')
with asyncio.Runner() as runner:
    runner.run(main())
													

Added in version 3.11.

run ( coro , * , context = None ) ¶

运行 协程 coro 在嵌入循环中。

返回协程结果 (或引发其异常)。

可选仅关键词 context 自变量允许指定自定义 contextvars.Context 为 coro 以在其中运行。使用 Runner 的默认上下文若为 None .

无法调用此函数,当另一 asyncio 事件循环在同一线程中运行时。

close ( ) ¶

关闭 Runner。

定稿异步生成器,关闭默认执行器,关闭事件循环及释放嵌入 contextvars.Context .

get_loop ( ) ¶

返回 Runner 实例关联的事件循环。

注意

Runner 使用惰性初始化战略,其构造函数不会初始化底层低级结构。

嵌入 loop and context 的创建是在 with 本体进入或首次调用 run() or get_loop() .

处理键盘中断 ¶

Added in version 3.11.

当 signal.SIGINT 被引发通过 Ctrl - C , KeyboardInterrupt exception is raised in the main thread by default. However this doesn’t work with asyncio because it can interrupt asyncio internals and can hang the program from exiting.

To mitigate this issue, asyncio 处理 signal.SIGINT 如下:

  1. asyncio.Runner.run() installs a custom signal.SIGINT handler before any user code is executed and removes it when exiting from the function.

  2. The Runner creates the main task for the passed coroutine for its execution.

  3. 当 signal.SIGINT 被引发通过 Ctrl - C , the custom signal handler cancels the main task by calling asyncio.Task.cancel() which raises asyncio.CancelledError inside the main task. This causes the Python stack to unwind, try/except and try/finally blocks can be used for resource cleanup. After the main task is cancelled, asyncio.Runner.run() 引发 KeyboardInterrupt .

  4. A user could write a tight loop which cannot be interrupted by asyncio.Task.cancel() , in which case the second following Ctrl - C immediately raises the KeyboardInterrupt without cancelling the main task.

内容表

  • 运行器
    • 运行异步程序
    • Runner 上下文管理器
    • 处理键盘中断

上一话题

asyncio — 异步 I/O

下一话题

协程和任务

本页

  • 报告 Bug
  • 展示源

快速搜索

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

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

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

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