blob: cc81001efbd3886e042d7cc567dd270ae28282a8 [file] [log] [blame]
Victor Stinneraea82292014-07-08 23:42:38 +02001.. currentmodule:: asyncio
2
3Event loops
4===========
5
6Event loop functions
7--------------------
8
9The following functions are convenient shortcuts to accessing the methods of the
10global policy. Note that this provides access to the default policy, unless an
11alternative policy was set by calling :func:`set_event_loop_policy` earlier in
12the execution of the process.
13
14.. function:: get_event_loop()
15
16 Equivalent to calling ``get_event_loop_policy().get_event_loop()``.
17
18.. function:: set_event_loop(loop)
19
20 Equivalent to calling ``get_event_loop_policy().set_event_loop(loop)``.
21
22.. function:: new_event_loop()
23
24 Equivalent to calling ``get_event_loop_policy().new_event_loop()``.
25
26
Victor Stinner778015b2014-07-11 12:13:39 +020027.. _asyncio-event-loops:
28
Victor Stinneraea82292014-07-08 23:42:38 +020029Available event loops
30---------------------
31
32asyncio currently provides two implementations of event loops:
33:class:`SelectorEventLoop` and :class:`ProactorEventLoop`.
34
35.. class:: SelectorEventLoop
36
37 Event loop based on the :mod:`selectors` module. Subclass of
38 :class:`BaseEventLoop`.
39
40 Use the most efficient selector available on the platform.
41
42.. class:: ProactorEventLoop
43
44 Proactor event loop for Windows using "I/O Completion Ports" aka IOCP.
45 Subclass of :class:`BaseEventLoop`.
46
47 Availability: Windows.
48
49 .. seealso::
50
51 `MSDN documentation on I/O Completion Ports
52 <http://msdn.microsoft.com/en-us/library/windows/desktop/aa365198%28v=vs.85%29.aspx>`_.
53
54Example to use a :class:`ProactorEventLoop` on Windows::
55
56 import asyncio, os
57
58 if os.name == 'nt':
59 loop = asyncio.ProactorEventLoop()
60 asyncio.set_event_loop(loop)
61
Victor Stinner778015b2014-07-11 12:13:39 +020062.. _asyncio-platform-support:
Victor Stinneraea82292014-07-08 23:42:38 +020063
64Platform support
65----------------
66
67The :mod:`asyncio` module has been designed to be portable, but each platform
68still has subtle differences and may not support all :mod:`asyncio` features.
69
70Windows
71^^^^^^^
72
73Common limits of Windows event loops:
74
75- :meth:`~BaseEventLoop.create_unix_server` and
Victor Stinner778015b2014-07-11 12:13:39 +020076 :meth:`~BaseEventLoop.create_unix_server` are not supported: the socket
77 family :data:`socket.AF_UNIX` is specific to UNIX
Victor Stinneraea82292014-07-08 23:42:38 +020078- :meth:`~BaseEventLoop.add_signal_handler` and
79 :meth:`~BaseEventLoop.remove_signal_handler` are not supported
Victor Stinner778015b2014-07-11 12:13:39 +020080- :meth:`EventLoopPolicy.set_child_watcher` is not supported.
81 :class:`ProactorEventLoop` supports subprocesses. It has only one
82 implementation to watch child processes, there is no need to configure it.
Victor Stinneraea82292014-07-08 23:42:38 +020083
84:class:`SelectorEventLoop` specific limits:
85
86- :class:`~selectors.SelectSelector` is used but it only supports sockets,
87 see the `MSDN documentation of select
Victor Stinner778015b2014-07-11 12:13:39 +020088 <http://msdn.microsoft.com/en-us/library/windows/desktop/ms740141%28v=vs.85%29.aspx>`_
Victor Stinneraea82292014-07-08 23:42:38 +020089- :meth:`~BaseEventLoop.add_reader` and :meth:`~BaseEventLoop.add_writer` only
90 accept file descriptors of sockets
Victor Stinner778015b2014-07-11 12:13:39 +020091- Pipes are not supported
92 (ex: :meth:`~BaseEventLoop.connect_read_pipe`,
93 :meth:`~BaseEventLoop.connect_write_pipe`)
94- :ref:`Subprocesses <asyncio-subprocess>` are not supported
95 (ex: :meth:`~BaseEventLoop.subprocess_exec`,
96 :meth:`~BaseEventLoop.subprocess_shell`)
Victor Stinneraea82292014-07-08 23:42:38 +020097
98:class:`ProactorEventLoop` specific limits:
99
100- SSL is not supported: :meth:`~BaseEventLoop.create_connection` and
101 :meth:`~BaseEventLoop.create_server` cannot be used with SSL for example
102- :meth:`~BaseEventLoop.create_datagram_endpoint` (UDP) is not supported
103- :meth:`~BaseEventLoop.add_reader` and :meth:`~BaseEventLoop.add_writer` are
104 not supported
105
106The resolution of the monotonic clock on Windows is usually around 15.6 msec.
Victor Stinner778015b2014-07-11 12:13:39 +0200107The best resolution is 0.5 msec. The resolution depends on the hardware
108(availability of `HPET
109<http://fr.wikipedia.org/wiki/High_Precision_Event_Timer>`_) and on the Windows
110configuration. See :ref:`asyncio delayed calls <asyncio-delayed-calls>`.
Victor Stinneraea82292014-07-08 23:42:38 +0200111
112
113Mac OS X
114^^^^^^^^
115
116Character devices like PTY are only well supported since Mavericks (Mac OS
11710.9). They are not supported at all on Mac OS 10.5 and older.
118
119On Mac OS 10.6, 10.7 and 10.8, the default event loop is
120:class:`SelectorEventLoop` which uses :class:`selectors.KqueueSelector`.
121:class:`selectors.KqueueSelector` does not support character devices on these
122versions. The :class:`SelectorEventLoop` can be used with
123:class:`~selectors.SelectSelector` or :class:`~selectors.PollSelector` to
124support character devices on these versions of Mac OS X. Example::
125
126 import asyncio
127 import selectors
128
129 selector = selectors.SelectSelector()
130 loop = asyncio.SelectorEventLoop(selector)
131 asyncio.set_event_loop(loop)
132
133
134Event loop policies and the default policy
135------------------------------------------
136
137Event loop management is abstracted with a *policy* pattern, to provide maximal
138flexibility for custom platforms and frameworks. Throughout the execution of a
139process, a single global policy object manages the event loops available to the
140process based on the calling context. A policy is an object implementing the
141:class:`AbstractEventLoopPolicy` interface.
142
143For most users of :mod:`asyncio`, policies never have to be dealt with
144explicitly, since the default global policy is sufficient.
145
146The default policy defines context as the current thread, and manages an event
147loop per thread that interacts with :mod:`asyncio`. The module-level functions
148:func:`get_event_loop` and :func:`set_event_loop` provide convenient access to
149event loops managed by the default policy.
150
151Event loop policy interface
152---------------------------
153
154An event loop policy must implement the following interface:
155
156.. class:: AbstractEventLoopPolicy
157
158 .. method:: get_event_loop()
159
160 Get the event loop for the current context. Returns an event loop object
161 implementing the :class:`BaseEventLoop` interface, or raises an exception in case
162 no event loop has been set for the current context and the current policy
163 does not specify to create one. It should never return ``None``.
164
165 .. method:: set_event_loop(loop)
166
167 Set the event loop for the current context to *loop*.
168
169 .. method:: new_event_loop()
170
171 Create and return a new event loop object according to this policy's rules.
172 If there's need to set this loop as the event loop for the current context,
173 :meth:`set_event_loop` must be called explicitly.
174
175Access to the global loop policy
176--------------------------------
177
178.. function:: get_event_loop_policy()
179
180 Get the current event loop policy.
181
182.. function:: set_event_loop_policy(policy)
183
184 Set the current event loop policy. If *policy* is ``None``, the default
185 policy is restored.
186