Python Tulip ( asyncio) 第4節 Event loops 事件循環

18.5.2. Event loops 事件循環

18.5.2.1. Event loop functions 事件循環函數

The following functions are convenient shortcuts to accessing the methods of the global policy. Note that this provides access to the default policy, unless an alternative policy was set by calling set_event_loop_policy() earlier in the execution of the process.

下面的函數是使用全局方法的一組快捷方式。注意僅僅是提供一個訪問默認規則,在沒有通過調用set_event_loop_policy()指定其他規則時有效。


  • asyncio.get_event_loop()

    Equivalent to calling get_event_loop_policy().get_event_loop().

   get_event_loop_policy().get_event_loop() 的等價調用。



  • asyncio.set_event_loop(loop)

    Equivalent to calling get_event_loop_policy().set_event_loop(loop).

     get_event_loop_policy().set_event_loop(loop)的等價調用。


  • asyncio.new_event_loop()

  • Equivalent to calling get_event_loop_policy().new_event_loop().

       get_event_loop_policy().new_event_loop()的等價調用。



18.5.2.2. Available event loops 可用的事件循環

asyncio currently provides two implementations of event loops: SelectorEventLoop and ProactorEventLoop.

asyncio目前提供兩種事件循環的實現:SelectorEventLoop ProactorEventLoop


  • class asyncio.SelectorEventLoop

    Event loop based on the selectors module. Subclass of BaseEventLoop.



    基於selectors 模塊的事件循環。是BaseEventLoop的子類。


    Use the most efficient selector available on the platform.

       會使用所有可用平臺上最高效的選擇器。


       On Windows, only sockets are supported (ex: pipes are not supported): see the MSDN documentation of select.

      在Windows,只有sockets模式被支持(並且管道不支持):見MSDN documentation of select


  • class asyncio.ProactorEventLoop

    Proactor event loop for Windows using “I/O Completion Ports” aka IOCP. Subclass of BaseEventLoop.

    用於Windows下,使用“IO Completion 端口”又稱IOCP的Proactor事件循環 ,是BaseEventLoop的子類。


    Availability: Windows. 可用性:Windows


    See also 參考 

    MSDN documentation on I/O Completion Ports.

Example to use a ProactorEventLoop on Windows:

在Windows上使用ProactorEventLoop 的示例:

import asyncio, os
if os.name == 'nt':
   loop = asyncio.ProactorEventLoop()
   asyncio.set_event_loop(loop)


18.5.2.3. Platform support 平臺支持

The asyncio module has been designed to be portable, but each platform still has subtle differences and may not support all asyncio features.

asyncio 模塊被設計成便於遷移的,但是每個平臺仍然有一些細微的區別,導致不能支持所有的asyncio 特性:


18.5.2.3.1. Windows

Common limits of Windows event loops:

Windows事件循環通常的限制:

       create_unix_connection() and create_unix_server()不被支持:socket.AF_UNIX端口族是UNIX規範。


       add_signal_handler() and remove_signal_handler()不被支持。



  • EventLoopPolicy.set_child_watcher() is not supported. ProactorEventLoop supports subprocesses. It has only one implementation to watch child processes, there is no need to configure it.

      EventLoopPolicy.set_child_watcher() 不支持。ProactorEventLoop 支持子進程。只有一個觀測子進程的實現,不需要進行配置。


SelectorEventLoop specific limits:  SelectorEventLoop標準限制

  • SelectSelector is used which only supports sockets and is limited to 512 sockets.

      SelectSelector只能用於socket,並且限制最多512個。

      add_reader() and add_writer()只能接受socket文件描述符。

      管道不支持(如:connect_read_pipe()connect_write_pipe())。

      Subprocesses不被支持(如:subprocess_exec()subprocess_shell())。

ProactorEventLoop specific limits:  ProactorEventLoop標準限制:

      create_datagram_endpoint() (UDP)不被支持。

      add_reader() and add_writer() 不被支持。

The resolution of the monotonic clock on Windows is usually around 15.6 msec. The best resolution is 0.5 msec. The resolution depends on the hardware (availability of HPET) and on the Windows configuration. See asyncio delayed calls.

Windows上絕對時鐘的解析度一般是15.6毫秒,最好的解析度爲0.5毫秒。解析度依賴於硬件(HPET的可用性)以及Windows配置。見asyncio delayed calls

Changed in version 3.5: ProactorEventLoop now supports SSL.  3.5版中的改變:ProactorEventLoop已支持SSL。


18.5.2.3.2. Mac OS X

Character devices like PTY are only well supported since Mavericks (Mac OS 10.9). They are not supported at all on Mac OS 10.5 and older.

需要Mavericks(MacOS 10.9)以上版本才能很好支持類似PTY字符設備。不能在MacOS 10.5及之前的版本中支持。


On Mac OS 10.6, 10.7 and 10.8, the default event loop is SelectorEventLoop which uses selectors.KqueueSelectorselectors.KqueueSelectordoes not support character devices on these versions. The SelectorEventLoop can be used with SelectSelector or PollSelector to support character devices on these versions of Mac OS X. Example:

在MacOS 10.6,10.7和10.8,默認的事件循環是SelectorEventLoop,它使用selectors.KqueueSelector 。 

selectors.KqueueSelector在這些版本不支持字符設備。可以SelectorEventLoop結合SelectSelector PollSelector用於支持這些版本的字符設備。例如:


import asyncio
import selectors
selector = selectors.SelectSelector()
loop = asyncio.SelectorEventLoop(selector)
asyncio.set_event_loop(loop)


18.5.2.4. Event loop policies and the default policy 事件循環規則和默認規則


Event loop management is abstracted with a policy pattern, to provide maximal flexibility for custom platforms and frameworks. Throughout the execution of a process, a single global policy object manages the event loops available to the process based on the calling context. A policy is an object implementing the AbstractEventLoopPolicy interface.


For most users of asyncio, policies never have to be dealt with explicitly, since the default global policy is sufficient.


The default policy defines context as the current thread, and manages an event loop per thread that interacts with asyncio. The module-level functionsget_event_loop() and set_event_loop() provide convenient access to event loops managed by the default policy.


18.5.2.5. Event loop policy interface 事件循環規則接口

An event loop policy must implement the following interface:

  • class asyncio.AbstractEventLoopPolicy

  • Event loop policy.

    • new_event_loop()

    • Create and return a new event loop object according to this policy’s rules.

      If there’s need to set this loop as the event loop for the current context, set_event_loop() must be called explicitly.

    • set_event_loop(loop)

    • Set the event loop for the current context to loop.

    • get_event_loop()

    • Get the event loop for the current context.

      Returns an event loop object implementing the BaseEventLoop interface.

      Raises an exception in case no event loop has been set for the current context and the current policy does not specify to create one. It must never return None.


18.5.2.6. Access to the global loop policy 訪問全局循環規則

  • asyncio.get_event_loop_policy()

  • Get the current event loop policy.

  • asyncio.set_event_loop_policy(policy)

  • Set the current event loop policy. If policy is None, the default policy is restored.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章