18.5.2. Event loops¶
Source code: Lib/asyncio/events.py
184.108.40.206. 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.
Equivalent to calling
Equivalent to calling
Equivalent to calling
220.127.116.11. Available event loops¶
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.
Proactor event loop for Windows using “I/O Completion Ports” aka IOCP. Subclass of
Example to use a
ProactorEventLoop on Windows:
import asyncio, sys if sys.platform == 'win32': loop = asyncio.ProactorEventLoop() asyncio.set_event_loop(loop)
18.104.22.168. Platform support¶
Common limits of Windows event loops:
create_unix_server()are not supported: the socket family
socket.AF_UNIXis specific to UNIX
remove_signal_handler()are not supported
EventLoopPolicy.set_child_watcher()is not supported.
ProactorEventLoopsupports subprocesses. It has only one implementation to watch child processes, there is no need to configure it.
SelectorEventLoop specific limits:
SelectSelectoris used which only supports sockets and is limited to 512 sockets.
add_writer()only accept file descriptors of sockets
- Pipes are not supported
- Subprocesses are not supported
ProactorEventLoop specific limits:
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.
Changed in version 3.5:
ProactorEventLoop now supports SSL.
22.214.171.124.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.
On Mac OS 10.6, 10.7 and 10.8, the default event loop is
SelectorEventLoop which uses
selectors.KqueueSelector does not support character devices on these
SelectorEventLoop can be used with
support character devices on these versions of Mac OS X. Example:
import asyncio import selectors selector = selectors.SelectSelector() loop = asyncio.SelectorEventLoop(selector) asyncio.set_event_loop(loop)
126.96.36.199. 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
For most users of
asyncio, policies never have to be dealt with
explicitly, since the default global policy is sufficient (see below).
188.8.131.52. Event loop policy interface¶
An event loop policy must implement the following interface:
Event loop policy.
Get the event loop for the current context.
Returns an event loop object implementing the
AbstractEventLoopinterface. In case called from coroutine, it returns the currently running event loop.
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
Changed in version 3.6.
Set the event loop for the current context to loop.
The default policy defines context as the current thread, and manages an event
loop per thread that interacts with
asyncio. If the current thread
doesn’t already have an event loop associated with it, the default policy’s
get_event_loop() method creates one when
called from the main thread, but raises
184.108.40.206. Access to the global loop policy¶
Get the current event loop policy.
Set the current event loop policy. If policy is
None, the default policy is restored.
220.127.116.11. Customizing the event loop policy¶
To implement a new event loop policy, it is recommended you subclass the
concrete default event loop policy
and override the methods for which you want to change behavior, for example:
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())