Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 3 | .. _asyncio-subprocess: |
| 4 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 5 | ============ |
| 6 | Subprocesses |
| 7 | ============ |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 8 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 9 | This section describes high-level async/await asyncio APIs to |
| 10 | create and manage subprocesses. |
lf | 627d2c8 | 2017-07-25 17:03:51 -0600 | [diff] [blame] | 11 | |
Yury Selivanov | 7372c3b | 2018-09-14 15:11:24 -0700 | [diff] [blame^] | 12 | .. _asyncio_example_subprocess_shell: |
| 13 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 14 | Here's an example of how asyncio can run a shell command and |
| 15 | communicate its result back:: |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 16 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 17 | import asyncio |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 18 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 19 | async def run(cmd): |
| 20 | proc = await asyncio.create_subprocess_shell( |
| 21 | cmd, |
| 22 | stdout=asyncio.subprocess.PIPE, |
| 23 | stderr=asyncio.subprocess.PIPE) |
Victor Stinner | aea8229 | 2014-07-08 23:42:38 +0200 | [diff] [blame] | 24 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 25 | stdout, stderr = await proc.communicate() |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 26 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 27 | print(f'[{cmd!r} exited with {proc.returncode}]') |
| 28 | if stdout: |
| 29 | print(f'[stdout]\n{stdout.decode()}') |
| 30 | if stderr: |
| 31 | print(f'[stderr]\n{stderr.decode()}') |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 32 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 33 | asyncio.run(run('ls /zzz')) |
| 34 | |
| 35 | will print:: |
| 36 | |
| 37 | ['ls /zzz' exited with 1] |
| 38 | [stderr] |
| 39 | ls: /zzz: No such file or directory |
| 40 | |
| 41 | Because all asyncio subprocess functions are asynchronous and asyncio |
| 42 | provides many tools to work with such functions, it is easy to execute |
| 43 | and monitor multiple subprocesses in parallel. It is indeed trivial |
| 44 | to modify the above example to run a few commands at once:: |
| 45 | |
| 46 | async def main(): |
| 47 | await asyncio.gather( |
| 48 | run('ls /zzz'), |
| 49 | run('sleep 1; echo "hello"')) |
| 50 | |
| 51 | asyncio.run(main()) |
| 52 | |
| 53 | See also the `Examples`_ subsection. |
Victor Stinner | 778015b | 2014-07-11 12:13:39 +0200 | [diff] [blame] | 54 | |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 55 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 56 | Creating Subprocesses |
| 57 | ===================== |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 58 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 59 | .. coroutinefunction:: create_subprocess_exec(\*args, stdin=None, \ |
| 60 | stdout=None, stderr=None, loop=None, \ |
| 61 | limit=None, \*\*kwds) |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 62 | |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 63 | Create a subprocess. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 64 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 65 | The *limit* argument sets the buffer limit for :class:`StreamReader` |
| 66 | wrappers for :attr:`Process.stdout` and :attr:`Process.stderr` |
Carol Willing | 4e824e9 | 2018-09-13 18:28:19 -0700 | [diff] [blame] | 67 | (if :attr:`subprocess.PIPE` is passed to *stdout* and *stderr* arguments). |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 68 | |
| 69 | Return a :class:`~asyncio.subprocess.Process` instance. |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 70 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 71 | See the documentation of :meth:`loop.subprocess_exec` for other |
| 72 | parameters. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 73 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 74 | .. coroutinefunction:: create_subprocess_shell(cmd, stdin=None, \ |
| 75 | stdout=None, stderr=None, loop=None, \ |
| 76 | limit=None, \*\*kwds) |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 77 | |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 78 | Run the shell command *cmd*. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 79 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 80 | The *limit* argument sets the buffer limit for :class:`StreamReader` |
| 81 | wrappers for :attr:`Process.stdout` and :attr:`Process.stderr` |
Carol Willing | 4e824e9 | 2018-09-13 18:28:19 -0700 | [diff] [blame] | 82 | (if :attr:`subprocess.PIPE` is passed to *stdout* and *stderr* arguments). |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 83 | |
| 84 | Return a :class:`~asyncio.subprocess.Process` instance. |
| 85 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 86 | See the documentation of :meth:`loop.subprocess_shell` for other |
| 87 | parameters. |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 88 | |
Carol Willing | 4e824e9 | 2018-09-13 18:28:19 -0700 | [diff] [blame] | 89 | .. important:: |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 90 | |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 91 | It is the application's responsibility to ensure that all whitespace and |
| 92 | metacharacters are quoted appropriately to avoid `shell injection |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 93 | <https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_ |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 94 | vulnerabilities. The :func:`shlex.quote` function can be used to properly |
| 95 | escape whitespace and shell metacharacters in strings that are going to be |
| 96 | used to construct shell commands. |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 97 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 98 | .. note:: |
| 99 | |
| 100 | The default event loop that asyncio is pre-configured |
Carol Willing | 4e824e9 | 2018-09-13 18:28:19 -0700 | [diff] [blame] | 101 | to use on **Windows** does not support subprocesses. Subprocesses are |
| 102 | available for Windows if the :class:`ProactorEventLoop` is used. |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 103 | See :ref:`Subprocess Support on Windows <asyncio-windows-subprocess>` |
| 104 | for details. |
Victor Stinner | 984600f | 2014-03-25 09:40:26 +0100 | [diff] [blame] | 105 | |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 106 | .. seealso:: |
| 107 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 108 | asyncio has also *low-level* APIs to work with subprocesses: |
| 109 | :meth:`loop.subprocess_exec`, :meth:`loop.subprocess_shell`, |
| 110 | :meth:`loop.connect_read_pipe`, :meth:`loop.connect_write_pipe`, |
| 111 | as well as the :ref:`Subprocess Transports <asyncio-subprocess-transports>` |
| 112 | and :ref:`Subprocess Protocols <asyncio-subprocess-protocols>`. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 113 | |
| 114 | |
| 115 | Constants |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 116 | ========= |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 117 | |
| 118 | .. data:: asyncio.subprocess.PIPE |
| 119 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 120 | Can be passed to the *stdin*, *stdout* or *stderr* parameters. |
| 121 | |
| 122 | If *PIPE* is passed to *stdin* argument, the |
| 123 | :attr:`Process.stdin <asyncio.subprocess.Process.stdin>` attribute |
| 124 | will point to a :class:`StreamWriter` instance. |
| 125 | |
| 126 | If *PIPE* is passed to *stdout* or *stderr* arguments, the |
| 127 | :attr:`Process.stdout <asyncio.subprocess.Process.stdout>` and |
| 128 | :attr:`Process.stderr <asyncio.subprocess.Process.stderr>` |
| 129 | attributes will point to :class:`StreamReader` instances. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 130 | |
| 131 | .. data:: asyncio.subprocess.STDOUT |
| 132 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 133 | Can be passed to the *stderr* parameter to redirect process' |
| 134 | *stderr* to *stdout*. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 135 | |
| 136 | .. data:: asyncio.subprocess.DEVNULL |
| 137 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 138 | Can be passed as the *stdin*, *stdout* or *stderr* parameters |
| 139 | to redirect the corresponding subprocess' IO to :data:`os.devnull`. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 140 | |
| 141 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 142 | Interacting with Subprocesses |
| 143 | ============================= |
| 144 | |
| 145 | Both :func:`create_subprocess_exec` and :func:`create_subprocess_shell` |
| 146 | functions return instances of the *Process* class. It is a high-level |
| 147 | wrapper that allows to watch for subprocesses completion and |
| 148 | communicate with them. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 149 | |
| 150 | .. class:: asyncio.subprocess.Process |
| 151 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 152 | An object that wraps OS processes created by the |
| 153 | :func:`create_subprocess_exec` and :func:`create_subprocess_shell` |
| 154 | functions. |
Victor Stinner | b79eb05 | 2014-02-03 23:08:14 +0100 | [diff] [blame] | 155 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 156 | This class is designed to have a similar API to the |
| 157 | :class:`subprocess.Popen` class, but there are some |
| 158 | notable differences: |
Victor Stinner | b79eb05 | 2014-02-03 23:08:14 +0100 | [diff] [blame] | 159 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 160 | * unlike Popen, Process instances do not have an equivalent to |
| 161 | the :meth:`~subprocess.Popen.poll` method; |
Victor Stinner | b79eb05 | 2014-02-03 23:08:14 +0100 | [diff] [blame] | 162 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 163 | * the :meth:`~asyncio.subprocess.Process.communicate` and |
| 164 | :meth:`~asyncio.subprocess.Process.wait` methods don't take a |
| 165 | *timeout* parameter: use the :func:`wait_for` function; |
| 166 | |
| 167 | * the :meth:`Process.wait() <asyncio.subprocess.Process.wait>` method |
| 168 | is asynchronous, whereas :meth:`subprocess.Popen.wait` method |
| 169 | is implemented as a blocking busy loop; |
| 170 | |
| 171 | * the *universal_newlines* parameter is not supported. |
| 172 | |
| 173 | This class is :ref:`not thread safe <asyncio-multithreading>`. |
| 174 | |
| 175 | See also the :ref:`Subprocess and Threads <asyncio-subprocess-threads>` |
| 176 | section. |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 177 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 178 | .. coroutinemethod:: wait() |
Victor Stinner | b79eb05 | 2014-02-03 23:08:14 +0100 | [diff] [blame] | 179 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 180 | Wait for child process to terminate. |
Victor Stinner | b79eb05 | 2014-02-03 23:08:14 +0100 | [diff] [blame] | 181 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 182 | Set and return the :attr:`returncode` attribute. |
Victor Stinner | b79eb05 | 2014-02-03 23:08:14 +0100 | [diff] [blame] | 183 | |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 184 | .. note:: |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 185 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 186 | This method can deadlock when using ``stdout=PIPE`` or |
| 187 | ``stderr=PIPE`` and the child process generates so much output |
| 188 | that it blocks waiting for the OS pipe buffer to accept |
| 189 | more data. Use the :meth:`communicate` method when using pipes |
| 190 | to avoid this condition. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 191 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 192 | .. coroutinemethod:: communicate(input=None) |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 193 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 194 | Interact with process: |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 195 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 196 | 1. send data to *stdin* (if *input* is not ``None``); |
| 197 | 2. read data from *stdout* and *stderr*, until EOF is reached; |
| 198 | 3. wait for process to terminate. |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 199 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 200 | The optional *input* argument is the data (:class:`bytes` object) |
| 201 | that will be sent to the child process. |
Victor Stinner | cc996b5 | 2014-07-17 12:25:27 +0200 | [diff] [blame] | 202 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 203 | Return a tuple ``(stdout_data, stderr_data)``. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 204 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 205 | If either :exc:`BrokenPipeError` or :exc:`ConnectionResetError` |
| 206 | exception is raised when writing *input* into *stdin*, the |
| 207 | exception is ignored. This condition occurs when the process |
| 208 | exits before all data are written into *stdin*. |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 209 | |
Carol Willing | 4e824e9 | 2018-09-13 18:28:19 -0700 | [diff] [blame] | 210 | If it is desired to send data to the process' *stdin*, |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 211 | the process needs to be created with ``stdin=PIPE``. Similarly, |
| 212 | to get anything other than ``None`` in the result tuple, the |
| 213 | process has to be created with ``stdout=PIPE`` and/or |
| 214 | ``stderr=PIPE`` arguments. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 215 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 216 | Note, that the data read is buffered in memory, so do not use |
| 217 | this method if the data size is large or unlimited. |
Victor Stinner | cc996b5 | 2014-07-17 12:25:27 +0200 | [diff] [blame] | 218 | |
Brian Curtin | a1afeec | 2014-02-08 18:36:14 -0600 | [diff] [blame] | 219 | .. method:: send_signal(signal) |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 220 | |
| 221 | Sends the signal *signal* to the child process. |
| 222 | |
| 223 | .. note:: |
| 224 | |
| 225 | On Windows, :py:data:`SIGTERM` is an alias for :meth:`terminate`. |
| 226 | ``CTRL_C_EVENT`` and ``CTRL_BREAK_EVENT`` can be sent to processes |
| 227 | started with a *creationflags* parameter which includes |
| 228 | ``CREATE_NEW_PROCESS_GROUP``. |
| 229 | |
| 230 | .. method:: terminate() |
| 231 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 232 | Stop the child. |
| 233 | |
| 234 | On Posix OSs the method sends :py:data:`signal.SIGTERM` to the |
| 235 | child process. |
| 236 | |
| 237 | On Windows the Win32 API function :c:func:`TerminateProcess` is |
| 238 | called to stop the child process. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 239 | |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 240 | .. method:: kill() |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 241 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 242 | Kill the child. |
| 243 | |
| 244 | On Posix OSs the function sends :py:data:`SIGKILL` to the child |
| 245 | process. |
| 246 | |
| 247 | On Windows this method is an alias for :meth:`terminate`. |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 248 | |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 249 | .. attribute:: stdin |
| 250 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 251 | Standard input stream (:class:`StreamWriter`) or ``None`` |
| 252 | if the process was created with ``stdin=None``. |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 253 | |
| 254 | .. attribute:: stdout |
| 255 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 256 | Standard output stream (:class:`StreamReader`) or ``None`` |
| 257 | if the process was created with ``stdout=None``. |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 258 | |
| 259 | .. attribute:: stderr |
| 260 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 261 | Standard error stream (:class:`StreamReader`) or ``None`` |
| 262 | if the process was created with ``stderr=None``. |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 263 | |
| 264 | .. warning:: |
| 265 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 266 | Use the :meth:`communicate` method rather than |
| 267 | :attr:`process.stdin.write() <stdin>`, |
| 268 | :attr:`await process.stdout.read() <stdout>` or |
Carol Willing | 4e824e9 | 2018-09-13 18:28:19 -0700 | [diff] [blame] | 269 | :attr:`await process.stderr.read <stderr>`. |
| 270 | This avoids deadlocks due to streams pausing reading or writing |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 271 | and blocking the child process. |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 272 | |
| 273 | .. attribute:: pid |
| 274 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 275 | Process identification number (PID). |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 276 | |
| 277 | Note that for processes created by the :func:`create_subprocess_shell` |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 278 | function, this attribute is the PID of the spawned shell. |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 279 | |
| 280 | .. attribute:: returncode |
| 281 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 282 | Return code of the process when it exits. |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 283 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 284 | A ``None`` value indicates that the process has not terminated yet. |
| 285 | |
| 286 | A negative value ``-N`` indicates that the child was terminated |
| 287 | by signal ``N`` (Unix only). |
Victor Stinner | 7bdf786 | 2014-03-16 21:29:31 +0100 | [diff] [blame] | 288 | |
Victor Stinner | e48d4db | 2014-02-03 23:26:28 +0100 | [diff] [blame] | 289 | |
Victor Stinner | 399c59d | 2015-01-09 01:32:02 +0100 | [diff] [blame] | 290 | .. _asyncio-subprocess-threads: |
| 291 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 292 | Subprocess and Threads |
Victor Stinner | 5492d35 | 2015-09-02 15:39:01 +0200 | [diff] [blame] | 293 | ---------------------- |
Victor Stinner | 399c59d | 2015-01-09 01:32:02 +0100 | [diff] [blame] | 294 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 295 | asyncio built-in event loops support running subprocesses from |
| 296 | different threads, but there are the following limitations: |
Victor Stinner | 399c59d | 2015-01-09 01:32:02 +0100 | [diff] [blame] | 297 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 298 | * An event loop must run in the main thread. |
Victor Stinner | 399c59d | 2015-01-09 01:32:02 +0100 | [diff] [blame] | 299 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 300 | * The child watcher must be instantiated in the main thread, |
| 301 | before executing subprocesses from other threads. Call the |
| 302 | :func:`get_child_watcher` function in the main thread to instantiate |
| 303 | the child watcher. |
| 304 | |
| 305 | Note, that alternative event loop implementations might not share |
| 306 | the above limitations; please refer to their documentation. |
Victor Stinner | 8370496 | 2015-02-25 14:24:15 +0100 | [diff] [blame] | 307 | |
Victor Stinner | 399c59d | 2015-01-09 01:32:02 +0100 | [diff] [blame] | 308 | .. seealso:: |
| 309 | |
| 310 | The :ref:`Concurrency and multithreading in asyncio |
| 311 | <asyncio-multithreading>` section. |
| 312 | |
| 313 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 314 | Examples |
| 315 | -------- |
Victor Stinner | e48d4db | 2014-02-03 23:26:28 +0100 | [diff] [blame] | 316 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 317 | An example using the :class:`~asyncio.subprocess.Process` class to |
| 318 | control a subprocess and the :class:`StreamReader` class to read from |
| 319 | the *stdout*. |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 320 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 321 | The subprocess is created by the :func:`create_subprocess_exec` |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 322 | function:: |
| 323 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 324 | import asyncio |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 325 | import sys |
| 326 | |
Mikhail Terekhov | d2ac400 | 2018-08-07 16:29:06 -0400 | [diff] [blame] | 327 | async def get_date(): |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 328 | code = 'import datetime; print(datetime.datetime.now())' |
| 329 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 330 | # Create the subprocess; redirect the standard output |
| 331 | # into a pipe. |
Andrew Svetlov | 8874342 | 2017-12-11 17:35:49 +0200 | [diff] [blame] | 332 | proc = await asyncio.create_subprocess_exec( |
| 333 | sys.executable, '-c', code, |
| 334 | stdout=asyncio.subprocess.PIPE) |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 335 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 336 | # Read one line of output. |
Andrew Svetlov | 8874342 | 2017-12-11 17:35:49 +0200 | [diff] [blame] | 337 | data = await proc.stdout.readline() |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 338 | line = data.decode('ascii').rstrip() |
| 339 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 340 | # Wait for the subprocess exit. |
Andrew Svetlov | 8874342 | 2017-12-11 17:35:49 +0200 | [diff] [blame] | 341 | await proc.wait() |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 342 | return line |
| 343 | |
| 344 | if sys.platform == "win32": |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 345 | asyncio.set_event_loop_policy( |
| 346 | asyncio.WindowsProactorEventLoopPolicy()) |
Victor Stinner | 3989205 | 2014-10-14 00:52:07 +0200 | [diff] [blame] | 347 | |
Yury Selivanov | 7c7605f | 2018-09-11 09:54:40 -0700 | [diff] [blame] | 348 | date = asyncio.run(get_date()) |
| 349 | print(f"Current date: {date}") |
| 350 | |
| 351 | |
| 352 | See also the :ref:`same example <asyncio-subprocess-proto-example>` |
| 353 | written using low-level APIs. |