内容表

  • 策略
    • 获取和设置策略
    • 策略对象
    • 进程看守程序
    • 就业培训     下载中心     Wiki     联络
      登录   注册

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

      策略 ¶

      An event loop policy is a global object used to get and set the current 事件循环 ,及创建新的事件循环。默认策略可以被 替换 with 内置替代 以使用不同事件循环实现,或被代入通过 自定义策略 可以覆盖这些行为。

      The 策略对象 gets and sets a separate event loop per context . This is per-thread by default, though custom policies could define context differently.

      Custom event loop policies can control the behavior of get_event_loop() , set_event_loop() ,和 new_event_loop() .

      Policy objects should implement the APIs defined in the AbstractEventLoopPolicy abstract base class.

      获取和设置策略 ¶

      The following functions can be used to get and set the policy for the current process:

      asyncio. get_event_loop_policy ( ) ¶

      返回当前进程范围的策略。

      asyncio. set_event_loop_policy ( policy ) ¶

      Set the current process-wide policy to policy .

      若 policy 被设为 None , the default policy is restored.

      策略对象 ¶

      The abstract event loop policy base class is defined as follows:

      class asyncio. AbstractEventLoopPolicy ¶

      An abstract base class for asyncio policies.

      get_event_loop ( ) ¶

      Get the event loop for the current context.

      Return an event loop object implementing the AbstractEventLoop 接口。

      This method should never return None .

      3.6 版改变。

      set_event_loop ( loop ) ¶

      Set the event loop for the current context to loop .

      new_event_loop ( ) ¶

      Create and return a new event loop object.

      This method should never return None .

      get_child_watcher ( ) ¶

      Get a child process watcher object.

      Return a watcher object implementing the AbstractChildWatcher 接口。

      This function is Unix specific.

      Deprecated since version 3.12.

      set_child_watcher ( watcher ) ¶

      Set the current child process watcher to watcher .

      This function is Unix specific.

      Deprecated since version 3.12.

      asyncio ships with the following built-in policies:

      class asyncio. DefaultEventLoopPolicy ¶

      The default asyncio policy. Uses SelectorEventLoop on Unix and ProactorEventLoop 在 Windows。

      There is no need to install the default policy manually. asyncio is configured to use the default policy automatically.

      3.8 版改变: 在 Windows, ProactorEventLoop is now used by default.

      Deprecated since version 3.12: The get_event_loop() method of the default asyncio policy now emits a DeprecationWarning if there is no current event loop set and it decides to create one. In some future Python release this will become an error.

      class asyncio. WindowsSelectorEventLoopPolicy ¶

      An alternative event loop policy that uses the SelectorEventLoop event loop implementation.

      可用性 :Windows。

      class asyncio. WindowsProactorEventLoopPolicy ¶

      An alternative event loop policy that uses the ProactorEventLoop event loop implementation.

      可用性 :Windows。

      进程看守程序 ¶

      A process watcher allows customization of how an event loop monitors child processes on Unix. Specifically, the event loop needs to know when a child process has exited.

      In asyncio, child processes are created with create_subprocess_exec() and loop.subprocess_exec() 函数。

      asyncio defines the AbstractChildWatcher abstract base class, which child watchers should implement, and has four different implementations: ThreadedChildWatcher (configured to be used by default), MultiLoopChildWatcher , SafeChildWatcher ,和 FastChildWatcher .

      另请参阅 子进程和线程 章节。

      The following two functions can be used to customize the child process watcher implementation used by the asyncio event loop:

      asyncio. get_child_watcher ( ) ¶

      Return the current child watcher for the current policy.

      Deprecated since version 3.12.

      asyncio. set_child_watcher ( watcher ) ¶

      Set the current child watcher to watcher for the current policy. watcher must implement methods defined in the AbstractChildWatcher 基类。

      Deprecated since version 3.12.

      注意

      Third-party event loops implementations might not support custom child watchers. For such event loops, using set_child_watcher() might be prohibited or have no effect.

      class asyncio. AbstractChildWatcher ¶
      add_child_handler ( pid , callback , * args ) ¶

      Register a new child handler.

      Arrange for callback(pid, returncode, *args) to be called when a process with PID equal to pid terminates. Specifying another callback for the same process replaces the previous handler.

      The callback callable must be thread-safe.

      remove_child_handler ( pid ) ¶

      Removes the handler for process with PID equal to pid .

      函数返回 True if the handler was successfully removed, False if there was nothing to remove.

      attach_loop ( loop ) ¶

      Attach the watcher to an event loop.

      If the watcher was previously attached to an event loop, then it is first detached before attaching to the new loop.

      Note: loop may be None .

      is_active ( ) ¶

      返回 True if the watcher is ready to use.

      Spawning a subprocess with inactive current child watcher raises RuntimeError .

      Added in version 3.8.

      close ( ) ¶

      Close the watcher.

      This method has to be called to ensure that underlying resources are cleaned-up.

      Deprecated since version 3.12.

      class asyncio. ThreadedChildWatcher ¶

      This implementation starts a new waiting thread for every subprocess spawn.

      It works reliably even when the asyncio event loop is run in a non-main OS thread.

      There is no noticeable overhead when handling a big number of children ( O (1) each time a child terminates), but starting a thread per process requires extra memory.

      This watcher is used by default.

      Added in version 3.8.

      class asyncio. MultiLoopChildWatcher ¶

      This implementation registers a SIGCHLD signal handler on instantiation. That can break third-party code that installs a custom handler for SIGCHLD 信号。

      The watcher avoids disrupting other code spawning processes by polling every process explicitly on a SIGCHLD 信号。

      There is no limitation for running subprocesses from different threads once the watcher is installed.

      The solution is safe but it has a significant overhead when handling a big number of processes ( O ( n ) each time a SIGCHLD is received).

      Added in version 3.8.

      Deprecated since version 3.12.

      class asyncio. SafeChildWatcher ¶

      This implementation uses active event loop from the main thread to handle SIGCHLD signal. If the main thread has no running event loop another thread cannot spawn a subprocess ( RuntimeError 被引发)。

      The watcher avoids disrupting other code spawning processes by polling every process explicitly on a SIGCHLD 信号。

      This solution is as safe as MultiLoopChildWatcher and has the same O ( n ) complexity but requires a running event loop in the main thread to work.

      Deprecated since version 3.12.

      class asyncio. FastChildWatcher ¶

      This implementation reaps every terminated processes by calling os.waitpid(-1) directly, possibly breaking other code spawning processes and waiting for their termination.

      There is no noticeable overhead when handling a big number of children ( O (1) each time a child terminates).

      This solution requires a running event loop in the main thread to work, as SafeChildWatcher .

      Deprecated since version 3.12.

      class asyncio. PidfdChildWatcher ¶

      This implementation polls process file descriptors (pidfds) to await child process termination. In some respects, PidfdChildWatcher is a “Goldilocks” child watcher implementation. It doesn’t require signals or threads, doesn’t interfere with any processes launched outside the event loop, and scales linearly with the number of subprocesses launched by the event loop. The main disadvantage is that pidfds are specific to Linux, and only work on recent (5.3+) kernels.

      Added in version 3.9.

      自定义策略 ¶

      To implement a new event loop policy, it is recommended to subclass DefaultEventLoopPolicy and override the methods for which custom behavior is wanted, e.g.:

      class MyEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
          def get_event_loop(self):
              """Get the event loop.
              This may be None or an instance of EventLoop.
              """
              loop = super().get_event_loop()
              # Do something with loop ...
              return loop
      asyncio.set_event_loop_policy(MyEventLoopPolicy())
      																						

      内容表

      • 策略
        • 获取和设置策略
        • 策略对象
        • 进程看守程序
        • 自定义策略

      上一话题

      传输和协议

      下一话题

      平台支持

      本页

      • 报告 Bug
      • 展示源

      快速搜索

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

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

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

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