blob: 3051fde5b93baf93c53e5f48012adf4e365e6c44 [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
Yury Selivanovabae67e2017-12-11 10:07:44 -050028.. function:: get_running_loop()
29
30 Return the running event loop in the current OS thread. If there
31 is no running event loop a :exc:`RuntimeError` is raised.
32
33 .. versionadded:: 3.7
34
Victor Stinneraea82292014-07-08 23:42:38 +020035
Victor Stinner778015b2014-07-11 12:13:39 +020036.. _asyncio-event-loops:
37
Victor Stinneraea82292014-07-08 23:42:38 +020038Available event loops
39---------------------
40
41asyncio currently provides two implementations of event loops:
42:class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
43
44.. class:: SelectorEventLoop
45
46 Event loop based on the :mod:`selectors` module. Subclass of
Guido van Rossumf68afd82016-08-08 09:41:21 -070047 :class:`AbstractEventLoop`.
Victor Stinneraea82292014-07-08 23:42:38 +020048
49 Use the most efficient selector available on the platform.
50
Victor Stinner41f3c3f2014-08-31 14:47:37 +020051 On Windows, only sockets are supported (ex: pipes are not supported):
52 see the `MSDN documentation of select
Georg Brandl5d941342016-02-26 19:37:12 +010053 <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740141%28v=vs.85%29.aspx>`_.
Victor Stinner41f3c3f2014-08-31 14:47:37 +020054
Victor Stinneraea82292014-07-08 23:42:38 +020055.. class:: ProactorEventLoop
56
57 Proactor event loop for Windows using "I/O Completion Ports" aka IOCP.
Guido van Rossumf68afd82016-08-08 09:41:21 -070058 Subclass of :class:`AbstractEventLoop`.
Victor Stinneraea82292014-07-08 23:42:38 +020059
60 Availability: Windows.
61
62 .. seealso::
63
64 `MSDN documentation on I/O Completion Ports
Georg Brandl5d941342016-02-26 19:37:12 +010065 <https://msdn.microsoft.com/en-us/library/windows/desktop/aa365198%28v=vs.85%29.aspx>`_.
Victor Stinneraea82292014-07-08 23:42:38 +020066
67Example to use a :class:`ProactorEventLoop` on Windows::
68
Guido van Rossum8778c6b2015-11-02 09:15:47 -080069 import asyncio, sys
Victor Stinneraea82292014-07-08 23:42:38 +020070
Guido van Rossum8778c6b2015-11-02 09:15:47 -080071 if sys.platform == 'win32':
Victor Stinneraea82292014-07-08 23:42:38 +020072 loop = asyncio.ProactorEventLoop()
73 asyncio.set_event_loop(loop)
74
Victor Stinner778015b2014-07-11 12:13:39 +020075.. _asyncio-platform-support:
Victor Stinneraea82292014-07-08 23:42:38 +020076
77Platform support
78----------------
79
80The :mod:`asyncio` module has been designed to be portable, but each platform
81still has subtle differences and may not support all :mod:`asyncio` features.
82
83Windows
84^^^^^^^
85
86Common limits of Windows event loops:
87
Guido van Rossumf68afd82016-08-08 09:41:21 -070088- :meth:`~AbstractEventLoop.create_unix_connection` and
89 :meth:`~AbstractEventLoop.create_unix_server` are not supported: the socket
Victor Stinner778015b2014-07-11 12:13:39 +020090 family :data:`socket.AF_UNIX` is specific to UNIX
Guido van Rossumf68afd82016-08-08 09:41:21 -070091- :meth:`~AbstractEventLoop.add_signal_handler` and
92 :meth:`~AbstractEventLoop.remove_signal_handler` are not supported
Victor Stinner778015b2014-07-11 12:13:39 +020093- :meth:`EventLoopPolicy.set_child_watcher` is not supported.
94 :class:`ProactorEventLoop` supports subprocesses. It has only one
95 implementation to watch child processes, there is no need to configure it.
Victor Stinneraea82292014-07-08 23:42:38 +020096
97:class:`SelectorEventLoop` specific limits:
98
Victor Stinner7eb10312015-01-09 15:59:44 +010099- :class:`~selectors.SelectSelector` is used which only supports sockets
100 and is limited to 512 sockets.
Guido van Rossumf68afd82016-08-08 09:41:21 -0700101- :meth:`~AbstractEventLoop.add_reader` and :meth:`~AbstractEventLoop.add_writer` only
Victor Stinneraea82292014-07-08 23:42:38 +0200102 accept file descriptors of sockets
Victor Stinner778015b2014-07-11 12:13:39 +0200103- Pipes are not supported
Guido van Rossumf68afd82016-08-08 09:41:21 -0700104 (ex: :meth:`~AbstractEventLoop.connect_read_pipe`,
105 :meth:`~AbstractEventLoop.connect_write_pipe`)
Victor Stinner778015b2014-07-11 12:13:39 +0200106- :ref:`Subprocesses <asyncio-subprocess>` are not supported
Guido van Rossumf68afd82016-08-08 09:41:21 -0700107 (ex: :meth:`~AbstractEventLoop.subprocess_exec`,
108 :meth:`~AbstractEventLoop.subprocess_shell`)
Victor Stinneraea82292014-07-08 23:42:38 +0200109
110:class:`ProactorEventLoop` specific limits:
111
Guido van Rossumf68afd82016-08-08 09:41:21 -0700112- :meth:`~AbstractEventLoop.create_datagram_endpoint` (UDP) is not supported
113- :meth:`~AbstractEventLoop.add_reader` and :meth:`~AbstractEventLoop.add_writer` are
Victor Stinneraea82292014-07-08 23:42:38 +0200114 not supported
115
116The resolution of the monotonic clock on Windows is usually around 15.6 msec.
Victor Stinner778015b2014-07-11 12:13:39 +0200117The best resolution is 0.5 msec. The resolution depends on the hardware
118(availability of `HPET
Georg Brandl5d941342016-02-26 19:37:12 +0100119<https://en.wikipedia.org/wiki/High_Precision_Event_Timer>`_) and on the Windows
Victor Stinner778015b2014-07-11 12:13:39 +0200120configuration. See :ref:`asyncio delayed calls <asyncio-delayed-calls>`.
Victor Stinneraea82292014-07-08 23:42:38 +0200121
Victor Stinner4ec04222015-01-14 00:30:22 +0100122.. versionchanged:: 3.5
123
124 :class:`ProactorEventLoop` now supports SSL.
125
Victor Stinneraea82292014-07-08 23:42:38 +0200126
127Mac OS X
128^^^^^^^^
129
130Character devices like PTY are only well supported since Mavericks (Mac OS
13110.9). They are not supported at all on Mac OS 10.5 and older.
132
133On Mac OS 10.6, 10.7 and 10.8, the default event loop is
134:class:`SelectorEventLoop` which uses :class:`selectors.KqueueSelector`.
135:class:`selectors.KqueueSelector` does not support character devices on these
136versions. The :class:`SelectorEventLoop` can be used with
137:class:`~selectors.SelectSelector` or :class:`~selectors.PollSelector` to
138support character devices on these versions of Mac OS X. Example::
139
140 import asyncio
141 import selectors
142
143 selector = selectors.SelectSelector()
144 loop = asyncio.SelectorEventLoop(selector)
145 asyncio.set_event_loop(loop)
146
147
148Event loop policies and the default policy
149------------------------------------------
150
151Event loop management is abstracted with a *policy* pattern, to provide maximal
152flexibility for custom platforms and frameworks. Throughout the execution of a
153process, a single global policy object manages the event loops available to the
154process based on the calling context. A policy is an object implementing the
155:class:`AbstractEventLoopPolicy` interface.
156
157For most users of :mod:`asyncio`, policies never have to be dealt with
Antoine Pitrou4135c892017-11-07 10:26:32 +0100158explicitly, since the default global policy is sufficient (see below).
Victor Stinneraea82292014-07-08 23:42:38 +0200159
Antoine Pitrou4135c892017-11-07 10:26:32 +0100160The module-level functions
Victor Stinneraea82292014-07-08 23:42:38 +0200161:func:`get_event_loop` and :func:`set_event_loop` provide convenient access to
162event loops managed by the default policy.
163
Victor Stinner1deee542014-11-28 13:58:28 +0100164
Victor Stinneraea82292014-07-08 23:42:38 +0200165Event loop policy interface
166---------------------------
167
168An event loop policy must implement the following interface:
169
170.. class:: AbstractEventLoopPolicy
171
Victor Stinner1deee542014-11-28 13:58:28 +0100172 Event loop policy.
173
Victor Stinneraea82292014-07-08 23:42:38 +0200174 .. method:: get_event_loop()
175
Victor Stinner1deee542014-11-28 13:58:28 +0100176 Get the event loop for the current context.
177
Guido van Rossumf68afd82016-08-08 09:41:21 -0700178 Returns an event loop object implementing the :class:`AbstractEventLoop`
Victor Stinner1deee542014-11-28 13:58:28 +0100179 interface.
180
181 Raises an exception in case no event loop has been set for the current
182 context and the current policy does not specify to create one. It must
183 never return ``None``.
Victor Stinneraea82292014-07-08 23:42:38 +0200184
185 .. method:: set_event_loop(loop)
186
Victor Stinner1deee542014-11-28 13:58:28 +0100187 Set the event loop for the current context to *loop*.
Victor Stinneraea82292014-07-08 23:42:38 +0200188
189 .. method:: new_event_loop()
190
Victor Stinner1deee542014-11-28 13:58:28 +0100191 Create and return a new event loop object according to this policy's
192 rules.
193
194 If there's need to set this loop as the event loop for the current
195 context, :meth:`set_event_loop` must be called explicitly.
196
Victor Stinneraea82292014-07-08 23:42:38 +0200197
Antoine Pitrou4135c892017-11-07 10:26:32 +0100198The default policy defines context as the current thread, and manages an event
Pablo Galindo77106b22017-12-10 17:34:59 +0000199loop per thread that interacts with :mod:`asyncio`. An exception to this rule
200happens when :meth:`~AbstractEventLoopPolicy.get_event_loop` is called from a
201running future/coroutine, in which case it will return the current loop
202running that future/coroutine.
203
204If the current thread doesn't already have an event loop associated with it,
205the default policy's :meth:`~AbstractEventLoopPolicy.get_event_loop` method
206creates one when called from the main thread, but raises :exc:`RuntimeError`
207otherwise.
Antoine Pitrou4135c892017-11-07 10:26:32 +0100208
209
Victor Stinneraea82292014-07-08 23:42:38 +0200210Access to the global loop policy
211--------------------------------
212
213.. function:: get_event_loop_policy()
214
215 Get the current event loop policy.
216
217.. function:: set_event_loop_policy(policy)
218
219 Set the current event loop policy. If *policy* is ``None``, the default
220 policy is restored.
Antoine Pitrou4135c892017-11-07 10:26:32 +0100221
222
223Customizing the event loop policy
224---------------------------------
225
226To implement a new event loop policy, it is recommended you subclass the
227concrete default event loop policy :class:`DefaultEventLoopPolicy`
228and override the methods for which you want to change behavior, for example::
229
230 class MyEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
231
232 def get_event_loop(self):
233 """Get the event loop.
234
235 This may be None or an instance of EventLoop.
236 """
237 loop = super().get_event_loop()
238 # Do something with loop ...
239 return loop
240
241 asyncio.set_event_loop_policy(MyEventLoopPolicy())