Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
| 3 | |
| 4 | .. _asyncio-policies: |
| 5 | |
| 6 | ======== |
| 7 | Policies |
| 8 | ======== |
| 9 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 10 | An event loop policy is a global per-process object that controls |
| 11 | the management of the event loop. Each event loop has a default |
| 12 | policy, which can be changed and customized using the policy API. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 13 | |
Carol Willing | 6d9767f | 2018-09-12 17:09:08 -0700 | [diff] [blame] | 14 | A policy defines the notion of *context* and manages a |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 15 | separate event loop per context. The default policy |
Carol Willing | 6d9767f | 2018-09-12 17:09:08 -0700 | [diff] [blame] | 16 | defines *context* to be the current thread. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 17 | |
| 18 | By using a custom event loop policy, the behavior of |
| 19 | :func:`get_event_loop`, :func:`set_event_loop`, and |
| 20 | :func:`new_event_loop` functions can be customized. |
| 21 | |
| 22 | Policy objects should implement the APIs defined |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 23 | in the :class:`AbstractEventLoopPolicy` abstract base class. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 24 | |
| 25 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 26 | Getting and Setting the Policy |
| 27 | ============================== |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 28 | |
| 29 | The following functions can be used to get and set the policy |
| 30 | for the current process: |
| 31 | |
| 32 | .. function:: get_event_loop_policy() |
| 33 | |
| 34 | Return the current process-wide policy. |
| 35 | |
| 36 | .. function:: set_event_loop_policy(policy) |
| 37 | |
| 38 | Set the current process-wide policy to *policy*. |
| 39 | |
| 40 | If *policy* is set to ``None``, the default policy is restored. |
| 41 | |
| 42 | |
| 43 | Policy Objects |
| 44 | ============== |
| 45 | |
| 46 | The abstract event loop policy base class is defined as follows: |
| 47 | |
| 48 | .. class:: AbstractEventLoopPolicy |
| 49 | |
| 50 | An abstract base class for asyncio policies. |
| 51 | |
| 52 | .. method:: get_event_loop() |
| 53 | |
| 54 | Get the event loop for the current context. |
| 55 | |
| 56 | Return an event loop object implementing the |
| 57 | :class:`AbstractEventLoop` interface. |
| 58 | |
| 59 | This method should never return ``None``. |
| 60 | |
| 61 | .. versionchanged:: 3.6 |
| 62 | |
| 63 | .. method:: set_event_loop(loop) |
| 64 | |
| 65 | Set the event loop for the current context to *loop*. |
| 66 | |
| 67 | .. method:: new_event_loop() |
| 68 | |
| 69 | Create and return a new event loop object. |
| 70 | |
| 71 | This method should never return ``None``. |
| 72 | |
| 73 | .. method:: get_child_watcher() |
| 74 | |
| 75 | Get a child process watcher object. |
| 76 | |
| 77 | Return a watcher object implementing the |
| 78 | :class:`AbstractChildWatcher` interface. |
| 79 | |
| 80 | This function is Unix specific. |
| 81 | |
| 82 | .. method:: set_child_watcher(watcher) |
| 83 | |
sth | 1b29c03 | 2018-12-30 23:01:28 +0100 | [diff] [blame] | 84 | Set the current child process watcher to *watcher*. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 85 | |
| 86 | This function is Unix specific. |
| 87 | |
| 88 | |
| 89 | asyncio ships with the following built-in policies: |
| 90 | |
| 91 | |
| 92 | .. class:: DefaultEventLoopPolicy |
| 93 | |
| 94 | The default asyncio policy. Uses :class:`SelectorEventLoop` |
Victor Stinner | 6ea29c5 | 2018-09-25 08:27:08 -0700 | [diff] [blame] | 95 | on Unix and :class:`ProactorEventLoop` on Windows. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 96 | |
Carol Willing | 6d9767f | 2018-09-12 17:09:08 -0700 | [diff] [blame] | 97 | There is no need to install the default policy manually. asyncio |
| 98 | is configured to use the default policy automatically. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 99 | |
Victor Stinner | 6ea29c5 | 2018-09-25 08:27:08 -0700 | [diff] [blame] | 100 | .. versionchanged:: 3.8 |
| 101 | |
| 102 | On Windows, :class:`ProactorEventLoop` is now used by default. |
| 103 | |
| 104 | |
| 105 | .. class:: WindowsSelectorEventLoopPolicy |
| 106 | |
| 107 | An alternative event loop policy that uses the |
| 108 | :class:`SelectorEventLoop` event loop implementation. |
| 109 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 110 | .. availability:: Windows. |
Victor Stinner | 6ea29c5 | 2018-09-25 08:27:08 -0700 | [diff] [blame] | 111 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 112 | |
| 113 | .. class:: WindowsProactorEventLoopPolicy |
| 114 | |
| 115 | An alternative event loop policy that uses the |
| 116 | :class:`ProactorEventLoop` event loop implementation. |
| 117 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 118 | .. availability:: Windows. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 119 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 120 | .. _asyncio-watchers: |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 121 | |
| 122 | Process Watchers |
| 123 | ================ |
| 124 | |
| 125 | A process watcher allows customization of how an event loop monitors |
| 126 | child processes on Unix. Specifically, the event loop needs to know |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 127 | when a child process has exited. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 128 | |
| 129 | In asyncio, child processes are created with |
| 130 | :func:`create_subprocess_exec` and :meth:`loop.subprocess_exec` |
| 131 | functions. |
| 132 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 133 | asyncio defines the :class:`AbstractChildWatcher` abstract base class, which child |
| 134 | watchers should implement, and has four different implementations: |
| 135 | :class:`ThreadedChildWatcher` (configured to be used by default), |
| 136 | :class:`MultiLoopChildWatcher`, :class:`SafeChildWatcher`, and |
| 137 | :class:`FastChildWatcher`. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 138 | |
| 139 | See also the :ref:`Subprocess and Threads <asyncio-subprocess-threads>` |
| 140 | section. |
| 141 | |
Carol Willing | 6d9767f | 2018-09-12 17:09:08 -0700 | [diff] [blame] | 142 | The following two functions can be used to customize the child process watcher |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 143 | implementation used by the asyncio event loop: |
| 144 | |
| 145 | .. function:: get_child_watcher() |
| 146 | |
| 147 | Return the current child watcher for the current policy. |
| 148 | |
| 149 | .. function:: set_child_watcher(watcher) |
| 150 | |
| 151 | Set the current child watcher to *watcher* for the current |
| 152 | policy. *watcher* must implement methods defined in the |
| 153 | :class:`AbstractChildWatcher` base class. |
| 154 | |
| 155 | .. note:: |
| 156 | Third-party event loops implementations might not support |
| 157 | custom child watchers. For such event loops, using |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 158 | :func:`set_child_watcher` might be prohibited or have no effect. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 159 | |
| 160 | .. class:: AbstractChildWatcher |
| 161 | |
| 162 | .. method:: add_child_handler(pid, callback, \*args) |
| 163 | |
| 164 | Register a new child handler. |
| 165 | |
| 166 | Arrange for ``callback(pid, returncode, *args)`` to be called |
| 167 | when a process with PID equal to *pid* terminates. Specifying |
| 168 | another callback for the same process replaces the previous |
| 169 | handler. |
| 170 | |
Elvis Pranskevichus | 1fa2ec4 | 2018-09-17 19:16:44 -0400 | [diff] [blame] | 171 | The *callback* callable must be thread-safe. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 172 | |
| 173 | .. method:: remove_child_handler(pid) |
| 174 | |
| 175 | Removes the handler for process with PID equal to *pid*. |
| 176 | |
| 177 | The function returns ``True`` if the handler was successfully |
| 178 | removed, ``False`` if there was nothing to remove. |
| 179 | |
| 180 | .. method:: attach_loop(loop) |
| 181 | |
| 182 | Attach the watcher to an event loop. |
| 183 | |
| 184 | If the watcher was previously attached to an event loop, then |
| 185 | it is first detached before attaching to the new loop. |
| 186 | |
| 187 | Note: loop may be ``None``. |
| 188 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 189 | .. method:: is_active() |
| 190 | |
| 191 | Return ``True`` if the watcher is ready to use. |
| 192 | |
| 193 | Spawning a subprocess with *inactive* current child watcher raises |
| 194 | :exc:`RuntimeError`. |
| 195 | |
| 196 | .. versionadded:: 3.8 |
| 197 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 198 | .. method:: close() |
| 199 | |
| 200 | Close the watcher. |
| 201 | |
| 202 | This method has to be called to ensure that underlying |
| 203 | resources are cleaned-up. |
| 204 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 205 | .. class:: ThreadedChildWatcher |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 206 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 207 | This implementation starts a new waiting thread for every subprocess spawn. |
| 208 | |
| 209 | It works reliably even when the asyncio event loop is run in a non-main OS thread. |
| 210 | |
| 211 | There is no noticeable overhead when handling a big number of children (*O(1)* each |
| 212 | time a child terminates), but stating a thread per process requires extra memory. |
| 213 | |
| 214 | This watcher is used by default. |
| 215 | |
| 216 | .. versionadded:: 3.8 |
| 217 | |
| 218 | .. class:: MultiLoopChildWatcher |
| 219 | |
| 220 | This implementation registers a :py:data:`SIGCHLD` signal handler on |
| 221 | instantiation. That can break third-party code that installs a custom handler for |
| 222 | `SIGCHLD`. signal). |
| 223 | |
| 224 | The watcher avoids disrupting other code spawning processes |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 225 | by polling every process explicitly on a :py:data:`SIGCHLD` signal. |
| 226 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 227 | There is no limitation for running subprocesses from different threads once the |
| 228 | watcher is installed. |
| 229 | |
| 230 | The solution is safe but it has a significant overhead when |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 231 | handling a big number of processes (*O(n)* each time a |
| 232 | :py:data:`SIGCHLD` is received). |
| 233 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 234 | .. versionadded:: 3.8 |
| 235 | |
| 236 | .. class:: SafeChildWatcher |
| 237 | |
| 238 | This implementation uses active event loop from the main thread to handle |
| 239 | :py:data:`SIGCHLD` signal. If the main thread has no running event loop another |
| 240 | thread cannot spawn a subprocess (:exc:`RuntimeError` is raised). |
| 241 | |
| 242 | The watcher avoids disrupting other code spawning processes |
| 243 | by polling every process explicitly on a :py:data:`SIGCHLD` signal. |
| 244 | |
| 245 | This solution is as safe as :class:`MultiLoopChildWatcher` and has the same *O(N)* |
| 246 | complexity but requires a running event loop in the main thread to work. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 247 | |
| 248 | .. class:: FastChildWatcher |
| 249 | |
| 250 | This implementation reaps every terminated processes by calling |
| 251 | ``os.waitpid(-1)`` directly, possibly breaking other code spawning |
| 252 | processes and waiting for their termination. |
| 253 | |
| 254 | There is no noticeable overhead when handling a big number of |
| 255 | children (*O(1)* each time a child terminates). |
| 256 | |
Andrew Svetlov | 0d671c0 | 2019-06-30 12:54:59 +0300 | [diff] [blame] | 257 | This solution requires a running event loop in the main thread to work, as |
| 258 | :class:`SafeChildWatcher`. |
| 259 | |
Benjamin Peterson | 3ccdd9b | 2019-11-13 19:08:50 -0800 | [diff] [blame] | 260 | .. class:: PidfdChildWatcher |
| 261 | |
| 262 | This implementation polls process file descriptors (pidfds) to await child |
| 263 | process termination. In some respects, :class:`PidfdChildWatcher` is a |
| 264 | "Goldilocks" child watcher implementation. It doesn't require signals or |
| 265 | threads, doesn't interfere with any processes launched outside the event |
| 266 | loop, and scales linearly with the number of subprocesses launched by the |
| 267 | event loop. The main disadvantage is that pidfds are specific to Linux, and |
| 268 | only work on recent (5.3+) kernels. |
| 269 | |
| 270 | .. versionadded:: 3.9 |
| 271 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 272 | |
| 273 | Custom Policies |
| 274 | =============== |
| 275 | |
| 276 | To implement a new event loop policy, it is recommended to subclass |
| 277 | :class:`DefaultEventLoopPolicy` and override the methods for which |
| 278 | custom behavior is wanted, e.g.:: |
| 279 | |
| 280 | class MyEventLoopPolicy(asyncio.DefaultEventLoopPolicy): |
| 281 | |
| 282 | def get_event_loop(self): |
| 283 | """Get the event loop. |
| 284 | |
| 285 | This may be None or an instance of EventLoop. |
| 286 | """ |
| 287 | loop = super().get_event_loop() |
| 288 | # Do something with loop ... |
| 289 | return loop |
| 290 | |
| 291 | asyncio.set_event_loop_policy(MyEventLoopPolicy()) |