您的位置:首页 > 编程语言 > Python开发

Python Tulip ( asyncio) 第4节 Event loops 事件循环

2015-02-09 14:21 423 查看

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目前提供两种事件循环的实现:SelectorEventLoopProactorEventLoop
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.KqueueSelectorselectors.KqueueSelector在这些版本不支持字符设备。可以SelectorEventLoop结合SelectSelectorPollSelector用于支持这些版本的字符设备。例如:
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: