blob: 7970e9039dfd86b8f785d39e20b110603f35dd41 [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
Antoine Pitrou4135c892017-11-07 10:26:32 +0100151explicitly, since the default global policy is sufficient (see below).
Victor Stinneraea82292014-07-08 23:42:38 +0200152
Antoine Pitrou4135c892017-11-07 10:26:32 +0100153The module-level functions
Victor Stinneraea82292014-07-08 23:42:38 +0200154:func:`get_event_loop` and :func:`set_event_loop` provide convenient access to
155event loops managed by the default policy.
156
Victor Stinner1deee542014-11-28 13:58:28 +0100157
Victor Stinneraea82292014-07-08 23:42:38 +0200158Event loop policy interface
159---------------------------
160
161An event loop policy must implement the following interface:
162
163.. class:: AbstractEventLoopPolicy
164
Victor Stinner1deee542014-11-28 13:58:28 +0100165 Event loop policy.
166
Victor Stinneraea82292014-07-08 23:42:38 +0200167 .. method:: get_event_loop()
168
Victor Stinner1deee542014-11-28 13:58:28 +0100169 Get the event loop for the current context.
170
Guido van Rossumf68afd82016-08-08 09:41:21 -0700171 Returns an event loop object implementing the :class:`AbstractEventLoop`
Victor Stinner1deee542014-11-28 13:58:28 +0100172 interface.
173
174 Raises an exception in case no event loop has been set for the current
175 context and the current policy does not specify to create one. It must
176 never return ``None``.
Victor Stinneraea82292014-07-08 23:42:38 +0200177
178 .. method:: set_event_loop(loop)
179
Victor Stinner1deee542014-11-28 13:58:28 +0100180 Set the event loop for the current context to *loop*.
Victor Stinneraea82292014-07-08 23:42:38 +0200181
182 .. method:: new_event_loop()
183
Victor Stinner1deee542014-11-28 13:58:28 +0100184 Create and return a new event loop object according to this policy's
185 rules.
186
187 If there's need to set this loop as the event loop for the current
188 context, :meth:`set_event_loop` must be called explicitly.
189
Victor Stinneraea82292014-07-08 23:42:38 +0200190
Antoine Pitrou4135c892017-11-07 10:26:32 +0100191The default policy defines context as the current thread, and manages an event
192loop per thread that interacts with :mod:`asyncio`. If the current thread
193doesn't already have an event loop associated with it, the default policy's
194:meth:`~AbstractEventLoopPolicy.get_event_loop` method creates one when
195called from the main thread, but raises :exc:`RuntimeError` otherwise.
196
197
Victor Stinneraea82292014-07-08 23:42:38 +0200198Access to the global loop policy
199--------------------------------
200
201.. function:: get_event_loop_policy()
202
203 Get the current event loop policy.
204
205.. function:: set_event_loop_policy(policy)
206
207 Set the current event loop policy. If *policy* is ``None``, the default
208 policy is restored.
Antoine Pitrou4135c892017-11-07 10:26:32 +0100209
210
211Customizing the event loop policy
212---------------------------------
213
214To implement a new event loop policy, it is recommended you subclass the
215concrete default event loop policy :class:`DefaultEventLoopPolicy`
216and override the methods for which you want to change behavior, for example::
217
218 class MyEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
219
220 def get_event_loop(self):
221 """Get the event loop.
222
223 This may be None or an instance of EventLoop.
224 """
225 loop = super().get_event_loop()
226 # Do something with loop ...
227 return loop
228
229 asyncio.set_event_loop_policy(MyEventLoopPolicy())