Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
| 3 | Event loops |
| 4 | =========== |
| 5 | |
| 6 | Event loop functions |
| 7 | -------------------- |
| 8 | |
| 9 | The following functions are convenient shortcuts to accessing the methods of the |
| 10 | global policy. Note that this provides access to the default policy, unless an |
| 11 | alternative policy was set by calling :func:`set_event_loop_policy` earlier in |
| 12 | the 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 Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 27 | .. _asyncio-event-loops: |
| 28 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 29 | Available event loops |
| 30 | --------------------- |
| 31 | |
| 32 | asyncio 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 | |
Victor Stinner | 41f3c3f | 2014-08-31 14:47:37 +0200 | [diff] [blame] | 42 | On Windows, only sockets are supported (ex: pipes are not supported): |
| 43 | see the `MSDN documentation of select |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 44 | <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] | 45 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 46 | .. class:: ProactorEventLoop |
| 47 | |
| 48 | Proactor event loop for Windows using "I/O Completion Ports" aka IOCP. |
| 49 | Subclass of :class:`BaseEventLoop`. |
| 50 | |
| 51 | Availability: Windows. |
| 52 | |
| 53 | .. seealso:: |
| 54 | |
| 55 | `MSDN documentation on I/O Completion Ports |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 56 | <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] | 57 | |
| 58 | Example to use a :class:`ProactorEventLoop` on Windows:: |
| 59 | |
Guido van Rossum | 8778c6b | 2015-11-02 09:15:47 -0800 | [diff] [blame] | 60 | import asyncio, sys |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 61 | |
Guido van Rossum | 8778c6b | 2015-11-02 09:15:47 -0800 | [diff] [blame] | 62 | if sys.platform == 'win32': |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 63 | loop = asyncio.ProactorEventLoop() |
| 64 | asyncio.set_event_loop(loop) |
| 65 | |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 66 | .. _asyncio-platform-support: |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 67 | |
| 68 | Platform support |
| 69 | ---------------- |
| 70 | |
| 71 | The :mod:`asyncio` module has been designed to be portable, but each platform |
| 72 | still has subtle differences and may not support all :mod:`asyncio` features. |
| 73 | |
| 74 | Windows |
| 75 | ^^^^^^^ |
| 76 | |
| 77 | Common limits of Windows event loops: |
| 78 | |
Victor Stinner | 33f6abe | 2014-10-12 20:36:04 +0200 | [diff] [blame] | 79 | - :meth:`~BaseEventLoop.create_unix_connection` and |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 80 | :meth:`~BaseEventLoop.create_unix_server` are not supported: the socket |
| 81 | family :data:`socket.AF_UNIX` is specific to UNIX |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 82 | - :meth:`~BaseEventLoop.add_signal_handler` and |
| 83 | :meth:`~BaseEventLoop.remove_signal_handler` are not supported |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 84 | - :meth:`EventLoopPolicy.set_child_watcher` is not supported. |
| 85 | :class:`ProactorEventLoop` supports subprocesses. It has only one |
| 86 | implementation to watch child processes, there is no need to configure it. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 87 | |
| 88 | :class:`SelectorEventLoop` specific limits: |
| 89 | |
Victor Stinner | 7eb1031 | 2015-01-09 15:59:44 +0100 | [diff] [blame] | 90 | - :class:`~selectors.SelectSelector` is used which only supports sockets |
| 91 | and is limited to 512 sockets. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 92 | - :meth:`~BaseEventLoop.add_reader` and :meth:`~BaseEventLoop.add_writer` only |
| 93 | accept file descriptors of sockets |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 94 | - Pipes are not supported |
| 95 | (ex: :meth:`~BaseEventLoop.connect_read_pipe`, |
| 96 | :meth:`~BaseEventLoop.connect_write_pipe`) |
| 97 | - :ref:`Subprocesses <asyncio-subprocess>` are not supported |
| 98 | (ex: :meth:`~BaseEventLoop.subprocess_exec`, |
| 99 | :meth:`~BaseEventLoop.subprocess_shell`) |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 100 | |
| 101 | :class:`ProactorEventLoop` specific limits: |
| 102 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 103 | - :meth:`~BaseEventLoop.create_datagram_endpoint` (UDP) is not supported |
| 104 | - :meth:`~BaseEventLoop.add_reader` and :meth:`~BaseEventLoop.add_writer` are |
| 105 | not supported |
| 106 | |
| 107 | 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] | 108 | The best resolution is 0.5 msec. The resolution depends on the hardware |
| 109 | (availability of `HPET |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 110 | <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] | 111 | configuration. See :ref:`asyncio delayed calls <asyncio-delayed-calls>`. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 112 | |
Victor Stinner | 4ec0422 | 2015-01-14 00:30:22 +0100 | [diff] [blame] | 113 | .. versionchanged:: 3.5 |
| 114 | |
| 115 | :class:`ProactorEventLoop` now supports SSL. |
| 116 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 117 | |
| 118 | Mac OS X |
| 119 | ^^^^^^^^ |
| 120 | |
| 121 | Character devices like PTY are only well supported since Mavericks (Mac OS |
| 122 | 10.9). They are not supported at all on Mac OS 10.5 and older. |
| 123 | |
| 124 | On Mac OS 10.6, 10.7 and 10.8, the default event loop is |
| 125 | :class:`SelectorEventLoop` which uses :class:`selectors.KqueueSelector`. |
| 126 | :class:`selectors.KqueueSelector` does not support character devices on these |
| 127 | versions. The :class:`SelectorEventLoop` can be used with |
| 128 | :class:`~selectors.SelectSelector` or :class:`~selectors.PollSelector` to |
| 129 | support character devices on these versions of Mac OS X. Example:: |
| 130 | |
| 131 | import asyncio |
| 132 | import selectors |
| 133 | |
| 134 | selector = selectors.SelectSelector() |
| 135 | loop = asyncio.SelectorEventLoop(selector) |
| 136 | asyncio.set_event_loop(loop) |
| 137 | |
| 138 | |
| 139 | Event loop policies and the default policy |
| 140 | ------------------------------------------ |
| 141 | |
| 142 | Event loop management is abstracted with a *policy* pattern, to provide maximal |
| 143 | flexibility for custom platforms and frameworks. Throughout the execution of a |
| 144 | process, a single global policy object manages the event loops available to the |
| 145 | process based on the calling context. A policy is an object implementing the |
| 146 | :class:`AbstractEventLoopPolicy` interface. |
| 147 | |
| 148 | For most users of :mod:`asyncio`, policies never have to be dealt with |
| 149 | explicitly, since the default global policy is sufficient. |
| 150 | |
| 151 | The default policy defines context as the current thread, and manages an event |
| 152 | loop per thread that interacts with :mod:`asyncio`. The module-level functions |
| 153 | :func:`get_event_loop` and :func:`set_event_loop` provide convenient access to |
| 154 | event loops managed by the default policy. |
| 155 | |
Victor Stinner | 1deee54 | 2014-11-28 13:58:28 +0100 | [diff] [blame] | 156 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 157 | Event loop policy interface |
| 158 | --------------------------- |
| 159 | |
| 160 | An event loop policy must implement the following interface: |
| 161 | |
| 162 | .. class:: AbstractEventLoopPolicy |
| 163 | |
Victor Stinner | 1deee54 | 2014-11-28 13:58:28 +0100 | [diff] [blame] | 164 | Event loop policy. |
| 165 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 166 | .. method:: get_event_loop() |
| 167 | |
Victor Stinner | 1deee54 | 2014-11-28 13:58:28 +0100 | [diff] [blame] | 168 | Get the event loop for the current context. |
| 169 | |
| 170 | Returns an event loop object implementing the :class:`BaseEventLoop` |
| 171 | interface. |
| 172 | |
| 173 | Raises an exception in case no event loop has been set for the current |
| 174 | context and the current policy does not specify to create one. It must |
| 175 | never return ``None``. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 176 | |
| 177 | .. method:: set_event_loop(loop) |
| 178 | |
Victor Stinner | 1deee54 | 2014-11-28 13:58:28 +0100 | [diff] [blame] | 179 | Set the event loop for the current context to *loop*. |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 180 | |
| 181 | .. method:: new_event_loop() |
| 182 | |
Victor Stinner | 1deee54 | 2014-11-28 13:58:28 +0100 | [diff] [blame] | 183 | Create and return a new event loop object according to this policy's |
| 184 | rules. |
| 185 | |
| 186 | If there's need to set this loop as the event loop for the current |
| 187 | context, :meth:`set_event_loop` must be called explicitly. |
| 188 | |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 189 | |
| 190 | Access to the global loop policy |
| 191 | -------------------------------- |
| 192 | |
| 193 | .. function:: get_event_loop_policy() |
| 194 | |
| 195 | Get the current event loop policy. |
| 196 | |
| 197 | .. function:: set_event_loop_policy(policy) |
| 198 | |
| 199 | Set the current event loop policy. If *policy* is ``None``, the default |
| 200 | policy is restored. |