设为首页 收藏本站
查看: 1295|回复: 0

[经验分享] Python Tulip ( asyncio) 第4节 Event loops 事件循环

[复制链接]

尚未签到

发表于 2018-8-12 14:13:52 | 显示全部楼层 |阅读模式
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>  下面的函数是使用全局方法的一组快捷方式。注意仅仅是提供一个访问默认规则,在没有通过调用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() are not supported: the socket family socket.AF_UNIX is specific to UNIX
  create_unix_connection() and create_unix_server()不被支持:socket.AF_UNIX端口族是UNIX规范。

  •   add_signal_handler() and remove_signal_handler() are not supported
  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() only accept file descriptors of sockets
  add_reader() and add_writer()只能接受socket文件描述符。

  •   Pipes are not supported (ex: connect_read_pipe(), connect_write_pipe())
  管道不支持(如:connect_read_pipe(), connect_write_pipe())。

  •   Subprocesses are not supported (ex: subprocess_exec(), subprocess_shell())
  Subprocesses不被支持(如:subprocess_exec(), subprocess_shell())。
  ProactorEventLoop specific limits:  ProactorEventLoop标准限制:

  •   create_datagram_endpoint() (UDP) is not supported
  create_datagram_endpoint() (UDP)不被支持。

  •   add_reader() and add_writer() are not supported
  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.KqueueSelector. selectors.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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-550707-1-1.html 上篇帖子: Python检测文本类型 下篇帖子: python学习方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表