Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
| 3 | Event loops |
| 4 | =========== |
| 5 | |
lf | 627d2c8 | 2017-07-25 17:03:51 -0600 | [diff] [blame] | 6 | **Source code:** :source:`Lib/asyncio/events.py` |
| 7 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 8 | Event loop functions |
| 9 | -------------------- |
| 10 | |
| 11 | The following functions are convenient shortcuts to accessing the methods of the |
| 12 | global policy. Note that this provides access to the default policy, unless an |
| 13 | alternative policy was set by calling :func:`set_event_loop_policy` earlier in |
| 14 | the 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 Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 29 | .. _asyncio-event-loops: |
| 30 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 31 | Available event loops |
| 32 | --------------------- |
| 33 | |
| 34 | asyncio 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 Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 40 | :class:`AbstractEventLoop`. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 41 | |
| 42 | Use the most efficient selector available on the platform. |
| 43 | |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 44 | On Windows, only sockets are supported (ex: pipes are not supported): |
| 45 | see the `MSDN documentation of select |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 46 | <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740141%28v=vs.85%29.aspx>`_. |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 47 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 48 | .. class:: ProactorEventLoop |
| 49 | |
| 50 | Proactor event loop for Windows using "I/O Completion Ports" aka IOCP. |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 51 | Subclass of :class:`AbstractEventLoop`. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 52 | |
| 53 | Availability: Windows. |
| 54 | |
| 55 | .. seealso:: |
| 56 | |
| 57 | `MSDN documentation on I/O Completion Ports |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 58 | <https://msdn.microsoft.com/en-us/library/windows/desktop/aa365198%28v=vs.85%29.aspx>`_. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 59 | |
| 60 | Example to use a :class:`ProactorEventLoop` on Windows:: |
| 61 | |
Guido van Rossum | 8778c6b | 2015-11-02 09:15:47 -0800 | [diff] [blame] | 62 | import asyncio, sys |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 63 | |
Guido van Rossum | 8778c6b | 2015-11-02 09:15:47 -0800 | [diff] [blame] | 64 | if sys.platform == 'win32': |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 65 | loop = asyncio.ProactorEventLoop() |
| 66 | asyncio.set_event_loop(loop) |
| 67 | |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 68 | .. _asyncio-platform-support: |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 69 | |
| 70 | Platform support |
| 71 | ---------------- |
| 72 | |
| 73 | The :mod:`asyncio` module has been designed to be portable, but each platform |
| 74 | still has subtle differences and may not support all :mod:`asyncio` features. |
| 75 | |
| 76 | Windows |
| 77 | ^^^^^^^ |
| 78 | |
| 79 | Common limits of Windows event loops: |
| 80 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 81 | - :meth:`~AbstractEventLoop.create_unix_connection` and |
| 82 | :meth:`~AbstractEventLoop.create_unix_server` are not supported: the socket |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 83 | family :data:`socket.AF_UNIX` is specific to UNIX |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 84 | - :meth:`~AbstractEventLoop.add_signal_handler` and |
| 85 | :meth:`~AbstractEventLoop.remove_signal_handler` are not supported |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 86 | - :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 Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 89 | |
| 90 | :class:`SelectorEventLoop` specific limits: |
| 91 | |
Victor Stinner | 7eb1031 | 2015-01-09 15:59:44 +0100 | [diff] [blame] | 92 | - :class:`~selectors.SelectSelector` is used which only supports sockets |
| 93 | and is limited to 512 sockets. |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 94 | - :meth:`~AbstractEventLoop.add_reader` and :meth:`~AbstractEventLoop.add_writer` only |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 95 | accept file descriptors of sockets |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 96 | - Pipes are not supported |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 97 | (ex: :meth:`~AbstractEventLoop.connect_read_pipe`, |
| 98 | :meth:`~AbstractEventLoop.connect_write_pipe`) |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 99 | - :ref:`Subprocesses <asyncio-subprocess>` are not supported |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 100 | (ex: :meth:`~AbstractEventLoop.subprocess_exec`, |
| 101 | :meth:`~AbstractEventLoop.subprocess_shell`) |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 102 | |
| 103 | :class:`ProactorEventLoop` specific limits: |
| 104 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 105 | - :meth:`~AbstractEventLoop.create_datagram_endpoint` (UDP) is not supported |
| 106 | - :meth:`~AbstractEventLoop.add_reader` and :meth:`~AbstractEventLoop.add_writer` are |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 107 | not supported |
| 108 | |
| 109 | The resolution of the monotonic clock on Windows is usually around 15.6 msec. |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 110 | The best resolution is 0.5 msec. The resolution depends on the hardware |
| 111 | (availability of `HPET |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 112 | <https://en.wikipedia.org/wiki/High_Precision_Event_Timer>`_) and on the Windows |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 113 | configuration. See :ref:`asyncio delayed calls <asyncio-delayed-calls>`. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 114 | |
Victor Stinner | 4ec0422 | 2015-01-14 00:30:22 +0100 | [diff] [blame] | 115 | .. versionchanged:: 3.5 |
| 116 | |
| 117 | :class:`ProactorEventLoop` now supports SSL. |
| 118 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 119 | |
| 120 | Mac OS X |
| 121 | ^^^^^^^^ |
| 122 | |
| 123 | Character devices like PTY are only well supported since Mavericks (Mac OS |
| 124 | 10.9). They are not supported at all on Mac OS 10.5 and older. |
| 125 | |
| 126 | On 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 |
| 129 | versions. The :class:`SelectorEventLoop` can be used with |
| 130 | :class:`~selectors.SelectSelector` or :class:`~selectors.PollSelector` to |
| 131 | support 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 | |
| 141 | Event loop policies and the default policy |
| 142 | ------------------------------------------ |
| 143 | |
| 144 | Event loop management is abstracted with a *policy* pattern, to provide maximal |
| 145 | flexibility for custom platforms and frameworks. Throughout the execution of a |
| 146 | process, a single global policy object manages the event loops available to the |
| 147 | process based on the calling context. A policy is an object implementing the |
| 148 | :class:`AbstractEventLoopPolicy` interface. |
| 149 | |
| 150 | For most users of :mod:`asyncio`, policies never have to be dealt with |
| 151 | explicitly, since the default global policy is sufficient. |
| 152 | |
| 153 | The default policy defines context as the current thread, and manages an event |
| 154 | loop 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 |
| 156 | event loops managed by the default policy. |
| 157 | |
Victor Stinner | 1deee54 | 2014-11-28 13:58:28 +0100 | [diff] [blame] | 158 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 159 | Event loop policy interface |
| 160 | --------------------------- |
| 161 | |
| 162 | An event loop policy must implement the following interface: |
| 163 | |
| 164 | .. class:: AbstractEventLoopPolicy |
| 165 | |
Victor Stinner | 1deee54 | 2014-11-28 13:58:28 +0100 | [diff] [blame] | 166 | Event loop policy. |
| 167 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 168 | .. method:: get_event_loop() |
| 169 | |
Victor Stinner | 1deee54 | 2014-11-28 13:58:28 +0100 | [diff] [blame] | 170 | Get the event loop for the current context. |
| 171 | |
Guido van Rossum | f68afd8 | 2016-08-08 09:41:21 -0700 | [diff] [blame] | 172 | Returns an event loop object implementing the :class:`AbstractEventLoop` |
Victor Stinner | 1deee54 | 2014-11-28 13:58:28 +0100 | [diff] [blame] | 173 | 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 Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 178 | |
| 179 | .. method:: set_event_loop(loop) |
| 180 | |
Victor Stinner | 1deee54 | 2014-11-28 13:58:28 +0100 | [diff] [blame] | 181 | Set the event loop for the current context to *loop*. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 182 | |
| 183 | .. method:: new_event_loop() |
| 184 | |
Victor Stinner | 1deee54 | 2014-11-28 13:58:28 +0100 | [diff] [blame] | 185 | 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 Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 191 | |
| 192 | Access 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. |