blob: d74fcb1e07f249e29d502dfc64cece6673d0292e [file] [log] [blame]
Victor Stinneraea82292014-07-08 23:42:38 +02001.. currentmodule:: asyncio
2
3Event loops
4===========
5
lf627d2c82017-07-25 17:03:51 -06006**Source code:** :source:`Lib/asyncio/events.py`
7
Victor Stinneraea82292014-07-08 23:42:38 +02008Event loop functions
9--------------------
10
11The following functions are convenient shortcuts to accessing the methods of the
12global policy. Note that this provides access to the default policy, unless an
13alternative policy was set by calling :func:`set_event_loop_policy` earlier in
14the execution of the process.
15
16.. function:: get_event_loop()
17
18 Equivalent to calling ``get_event_loop_policy().get_event_loop()``.
19
20.. function:: set_event_loop(loop)
21
22 Equivalent to calling ``get_event_loop_policy().set_event_loop(loop)``.
23
24.. function:: new_event_loop()
25
26 Equivalent to calling ``get_event_loop_policy().new_event_loop()``.
27
28
Victor Stinner778015b2014-07-11 12:13:39 +020029.. _asyncio-event-loops:
30
Victor Stinneraea82292014-07-08 23:42:38 +020031Available event loops
32---------------------
33
34asyncio currently provides two implementations of event loops:
35:class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
36
37.. class:: SelectorEventLoop
38
39 Event loop based on the :mod:`selectors` module. Subclass of
Guido van Rossumf68afd82016-08-08 09:41:21 -070040 :class:`AbstractEventLoop`.
Victor Stinneraea82292014-07-08 23:42:38 +020041
42 Use the most efficient selector available on the platform.
43
Victor Stinner41f3c3f2014-08-31 14:47:37 +020044 On Windows, only sockets are supported (ex: pipes are not supported):
45 see the `MSDN documentation of select
Georg Brandl5d941342016-02-26 19:37:12 +010046 <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740141%28v=vs.85%29.aspx>`_.
Victor Stinner41f3c3f2014-08-31 14:47:37 +020047
Victor Stinneraea82292014-07-08 23:42:38 +020048.. class:: ProactorEventLoop
49
50 Proactor event loop for Windows using "I/O Completion Ports" aka IOCP.
Guido van Rossumf68afd82016-08-08 09:41:21 -070051 Subclass of :class:`AbstractEventLoop`.
Victor Stinneraea82292014-07-08 23:42:38 +020052
53 Availability: Windows.
54
55 .. seealso::
56
57 `MSDN documentation on I/O Completion Ports
Georg Brandl5d941342016-02-26 19:37:12 +010058 <https://msdn.microsoft.com/en-us/library/windows/desktop/aa365198%28v=vs.85%29.aspx>`_.
Victor Stinneraea82292014-07-08 23:42:38 +020059
60Example to use a :class:`ProactorEventLoop` on Windows::
61
Guido van Rossum8778c6b2015-11-02 09:15:47 -080062 import asyncio, sys
Victor Stinneraea82292014-07-08 23:42:38 +020063
Guido van Rossum8778c6b2015-11-02 09:15:47 -080064 if sys.platform == 'win32':
Victor Stinneraea82292014-07-08 23:42:38 +020065 loop = asyncio.ProactorEventLoop()
66 asyncio.set_event_loop(loop)
67
Victor Stinner778015b2014-07-11 12:13:39 +020068.. _asyncio-platform-support:
Victor Stinneraea82292014-07-08 23:42:38 +020069
70Platform support
71----------------
72
73The :mod:`asyncio` module has been designed to be portable, but each platform
74still has subtle differences and may not support all :mod:`asyncio` features.
75
76Windows
77^^^^^^^
78
79Common limits of Windows event loops:
80
Guido van Rossumf68afd82016-08-08 09:41:21 -070081- :meth:`~AbstractEventLoop.create_unix_connection` and
82 :meth:`~AbstractEventLoop.create_unix_server` are not supported: the socket
Victor Stinner778015b2014-07-11 12:13:39 +020083 family :data:`socket.AF_UNIX` is specific to UNIX
Guido van Rossumf68afd82016-08-08 09:41:21 -070084- :meth:`~AbstractEventLoop.add_signal_handler` and
85 :meth:`~AbstractEventLoop.remove_signal_handler` are not supported
Victor Stinner778015b2014-07-11 12:13:39 +020086- :meth:`EventLoopPolicy.set_child_watcher` is not supported.
87 :class:`ProactorEventLoop` supports subprocesses. It has only one
88 implementation to watch child processes, there is no need to configure it.
Victor Stinneraea82292014-07-08 23:42:38 +020089
90:class:`SelectorEventLoop` specific limits:
91
Victor Stinner7eb10312015-01-09 15:59:44 +010092- :class:`~selectors.SelectSelector` is used which only supports sockets
93 and is limited to 512 sockets.
Guido van Rossumf68afd82016-08-08 09:41:21 -070094- :meth:`~AbstractEventLoop.add_reader` and :meth:`~AbstractEventLoop.add_writer` only
Victor Stinneraea82292014-07-08 23:42:38 +020095 accept file descriptors of sockets
Victor Stinner778015b2014-07-11 12:13:39 +020096- Pipes are not supported
Guido van Rossumf68afd82016-08-08 09:41:21 -070097 (ex: :meth:`~AbstractEventLoop.connect_read_pipe`,
98 :meth:`~AbstractEventLoop.connect_write_pipe`)
Victor Stinner778015b2014-07-11 12:13:39 +020099- :ref:`Subprocesses <asyncio-subprocess>` are not supported
Guido van Rossumf68afd82016-08-08 09:41:21 -0700100 (ex: :meth:`~AbstractEventLoop.subprocess_exec`,
101 :meth:`~AbstractEventLoop.subprocess_shell`)
Victor Stinneraea82292014-07-08 23:42:38 +0200102
103:class:`ProactorEventLoop` specific limits:
104
Guido van Rossumf68afd82016-08-08 09:41:21 -0700105- :meth:`~AbstractEventLoop.create_datagram_endpoint` (UDP) is not supported
106- :meth:`~AbstractEventLoop.add_reader` and :meth:`~AbstractEventLoop.add_writer` are
Victor Stinneraea82292014-07-08 23:42:38 +0200107 not supported
108
109The resolution of the monotonic clock on Windows is usually around 15.6 msec.
Victor Stinner778015b2014-07-11 12:13:39 +0200110The best resolution is 0.5 msec. The resolution depends on the hardware
111(availability of `HPET
Georg Brandl5d941342016-02-26 19:37:12 +0100112<https://en.wikipedia.org/wiki/High_Precision_Event_Timer>`_) and on the Windows
Victor Stinner778015b2014-07-11 12:13:39 +0200113configuration. See :ref:`asyncio delayed calls <asyncio-delayed-calls>`.
Victor Stinneraea82292014-07-08 23:42:38 +0200114
Victor Stinner4ec04222015-01-14 00:30:22 +0100115.. versionchanged:: 3.5
116
117 :class:`ProactorEventLoop` now supports SSL.
118
Victor Stinneraea82292014-07-08 23:42:38 +0200119
120Mac OS X
121^^^^^^^^
122
123Character devices like PTY are only well supported since Mavericks (Mac OS
12410.9). They are not supported at all on Mac OS 10.5 and older.
125
126On Mac OS 10.6, 10.7 and 10.8, the default event loop is
127:class:`SelectorEventLoop` which uses :class:`selectors.KqueueSelector`.
128:class:`selectors.KqueueSelector` does not support character devices on these
129versions. The :class:`SelectorEventLoop` can be used with
130:class:`~selectors.SelectSelector` or :class:`~selectors.PollSelector` to
131support character devices on these versions of Mac OS X. Example::
132
133 import asyncio
134 import selectors
135
136 selector = selectors.SelectSelector()
137 loop = asyncio.SelectorEventLoop(selector)
138 asyncio.set_event_loop(loop)
139
140
141Event loop policies and the default policy
142------------------------------------------
143
144Event loop management is abstracted with a *policy* pattern, to provide maximal
145flexibility for custom platforms and frameworks. Throughout the execution of a
146process, a single global policy object manages the event loops available to the
147process based on the calling context. A policy is an object implementing the
148:class:`AbstractEventLoopPolicy` interface.
149
150For most users of :mod:`asyncio`, policies never have to be dealt with
151explicitly, since the default global policy is sufficient.
152
153The default policy defines context as the current thread, and manages an event
154loop per thread that interacts with :mod:`asyncio`. The module-level functions
155:func:`get_event_loop` and :func:`set_event_loop` provide convenient access to
156event loops managed by the default policy.
157
Victor Stinner1deee542014-11-28 13:58:28 +0100158
Victor Stinneraea82292014-07-08 23:42:38 +0200159Event loop policy interface
160---------------------------
161
162An event loop policy must implement the following interface:
163
164.. class:: AbstractEventLoopPolicy
165
Victor Stinner1deee542014-11-28 13:58:28 +0100166 Event loop policy.
167
Victor Stinneraea82292014-07-08 23:42:38 +0200168 .. method:: get_event_loop()
169
Victor Stinner1deee542014-11-28 13:58:28 +0100170 Get the event loop for the current context.
171
Guido van Rossumf68afd82016-08-08 09:41:21 -0700172 Returns an event loop object implementing the :class:`AbstractEventLoop`
Victor Stinner1deee542014-11-28 13:58:28 +0100173 interface.
174
175 Raises an exception in case no event loop has been set for the current
176 context and the current policy does not specify to create one. It must
177 never return ``None``.
Victor Stinneraea82292014-07-08 23:42:38 +0200178
179 .. method:: set_event_loop(loop)
180
Victor Stinner1deee542014-11-28 13:58:28 +0100181 Set the event loop for the current context to *loop*.
Victor Stinneraea82292014-07-08 23:42:38 +0200182
183 .. method:: new_event_loop()
184
Victor Stinner1deee542014-11-28 13:58:28 +0100185 Create and return a new event loop object according to this policy's
186 rules.
187
188 If there's need to set this loop as the event loop for the current
189 context, :meth:`set_event_loop` must be called explicitly.
190
Victor Stinneraea82292014-07-08 23:42:38 +0200191
192Access to the global loop policy
193--------------------------------
194
195.. function:: get_event_loop_policy()
196
197 Get the current event loop policy.
198
199.. function:: set_event_loop_policy(policy)
200
201 Set the current event loop policy. If *policy* is ``None``, the default
202 policy is restored.