内容表

  • 延伸
    • 编写自定义事件循环
    • 未来和任务私有构造函数
    • 就业培训     下载中心     Wiki     联络
      登录   注册

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

      延伸 ¶

      The main direction for asyncio extending is writing custom 事件循环 classes. Asyncio has helpers that could be used to simplify this task.

      注意

      Third-parties should reuse existing asyncio code with caution, a new Python version is free to break backward compatibility in internal part of API.

      编写自定义事件循环 ¶

      asyncio.AbstractEventLoop declares very many methods. Implementing all them from scratch is a tedious job.

      A loop can get many common methods implementation for free by inheriting from asyncio.BaseEventLoop .

      In turn, the successor should implement a bunch of private methods declared but not implemented in asyncio.BaseEventLoop .

      例如, loop.create_connection() checks arguments, resolves DNS addresses, and calls loop._make_socket_transport() that should be implemented by inherited class. The _make_socket_transport() method is not documented and is considered as an internal API。

      未来和任务私有构造函数 ¶

      asyncio.Future and asyncio.Task should be never created directly, please use corresponding loop.create_future() and loop.create_task() ,或 asyncio.create_task() factories instead.

      However, third-party 事件循环 may reuse built-in future and task implementations for the sake of getting a complex and highly optimized code for free.

      For this purpose the following, private constructors are listed:

      Future. __init__ ( * , loop = None ) ¶

      创建内置未来实例。

      loop is an optional event loop instance.

      Task. __init__ ( coro , * , loop = None , name = None , context = None ) ¶

      创建内置任务实例。

      loop is an optional event loop instance. The rest of arguments are described in loop.create_task() description.

      3.11 版改变: context 自变量被添加。

      支持任务寿命 ¶

      A third party task implementation should call the following functions to keep a task visible by asyncio.all_tasks() and asyncio.current_task() :

      asyncio. _register_task ( task ) ¶

      Register a new task as managed by asyncio .

      从任务构造函数调用函数。

      asyncio. _unregister_task ( task ) ¶

      Unregister a task from asyncio internal structures.

      The function should be called when a task is about to finish.

      asyncio. _enter_task ( loop , task ) ¶

      Switch the current task to the task 自变量。

      Call the function just before executing a portion of embedded 协程 ( coroutine.send() or coroutine.throw() ).

      asyncio. _leave_task ( loop , task ) ¶

      Switch the current task back from task to None .

      Call the function just after coroutine.send() or coroutine.throw() execution.

      内容表

      • 延伸
        • 编写自定义事件循环
        • 未来和任务私有构造函数
        • 支持任务寿命

      上一话题

      平台支持

      下一话题

      高级 API 索引

      本页

      • 报告 Bug
      • 展示源

      快速搜索

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

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

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

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