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