Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
| 3 | Subprocess |
| 4 | ========== |
| 5 | |
Brian Curtin | a1afeec | 2014-02-08 18:36:14 -0600 | [diff] [blame] | 6 | Create a subprocess |
| 7 | ------------------- |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 8 | |
| 9 | .. function:: create_subprocess_shell(cmd, stdin=None, stdout=None, stderr=None, loop=None, limit=None, \*\*kwds) |
| 10 | |
Benjamin Peterson | 233eac4 | 2014-02-03 14:08:00 -0500 | [diff] [blame] | 11 | Run the shell command *cmd* given as a string. Return a :class:`Process` |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 12 | instance. |
| 13 | |
| 14 | This function returns a :ref:`coroutine object <coroutine>`. |
| 15 | |
| 16 | .. function:: create_subprocess_exec(\*args, stdin=None, stdout=None, stderr=None, loop=None, limit=None, \*\*kwds) |
| 17 | |
| 18 | Create a subprocess. Return a :class:`Process` instance. |
| 19 | |
| 20 | This function returns a :ref:`coroutine object <coroutine>`. |
| 21 | |
| 22 | Use the :meth:`BaseEventLoop.connect_read_pipe` and |
| 23 | :meth:`BaseEventLoop.connect_write_pipe` methods to connect pipes. |
| 24 | |
| 25 | .. seealso:: |
| 26 | |
| 27 | The :meth:`BaseEventLoop.subprocess_exec` and |
| 28 | :meth:`BaseEventLoop.subprocess_shell` methods. |
| 29 | |
| 30 | |
| 31 | Constants |
| 32 | --------- |
| 33 | |
| 34 | .. data:: asyncio.subprocess.PIPE |
| 35 | |
| 36 | Special value that can be used as the *stdin*, *stdout* or *stderr* argument |
| 37 | to :func:`create_subprocess_shell` and :func:`create_subprocess_exec` and |
| 38 | indicates that a pipe to the standard stream should be opened. |
| 39 | |
| 40 | .. data:: asyncio.subprocess.STDOUT |
| 41 | |
| 42 | Special value that can be used as the *stderr* argument to |
| 43 | :func:`create_subprocess_shell` and :func:`create_subprocess_exec` and |
| 44 | indicates that standard error should go into the same handle as standard |
| 45 | output. |
| 46 | |
| 47 | .. data:: asyncio.subprocess.DEVNULL |
| 48 | |
| 49 | Special value that can be used as the *stderr* argument to |
| 50 | :func:`create_subprocess_shell` and :func:`create_subprocess_exec` and |
| 51 | indicates that standard error should go into the same handle as standard |
| 52 | output. |
| 53 | |
| 54 | |
| 55 | Process |
| 56 | ------- |
| 57 | |
| 58 | .. class:: asyncio.subprocess.Process |
| 59 | |
Victor Stinner | b79eb05 | 2014-02-03 23:08:14 +0100 | [diff] [blame] | 60 | .. attribute:: pid |
| 61 | |
| 62 | The identifier of the process. |
| 63 | |
| 64 | Note that if you set the *shell* argument to ``True``, this is the |
| 65 | process identifier of the spawned shell. |
| 66 | |
| 67 | .. attribute:: returncode |
| 68 | |
| 69 | Return code of the process when it exited. A ``None`` value indicates |
| 70 | that the process has not terminated yet. |
| 71 | |
| 72 | A negative value ``-N`` indicates that the child was terminated by signal |
| 73 | ``N`` (Unix only). |
| 74 | |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 75 | .. attribute:: stdin |
| 76 | |
| 77 | Standard input stream (write), ``None`` if the process was created with |
| 78 | ``stdin=None``. |
| 79 | |
| 80 | .. attribute:: stdout |
| 81 | |
| 82 | Standard output stream (read), ``None`` if the process was created with |
| 83 | ``stdout=None``. |
| 84 | |
| 85 | .. attribute:: stderr |
| 86 | |
| 87 | Standard error stream (read), ``None`` if the process was created with |
| 88 | ``stderr=None``. |
| 89 | |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 90 | .. method:: communicate(input=None) |
| 91 | |
| 92 | Interact with process: Send data to stdin. Read data from stdout and |
| 93 | stderr, until end-of-file is reached. Wait for process to terminate. |
| 94 | The optional *input* argument should be data to be sent to the child |
| 95 | process, or ``None``, if no data should be sent to the child. The type |
| 96 | of *input* must be bytes. |
| 97 | |
| 98 | :meth:`communicate` returns a tuple ``(stdoutdata, stderrdata)``. |
| 99 | |
| 100 | Note that if you want to send data to the process's stdin, you need to |
Victor Stinner | 0c3949c | 2014-02-09 02:51:40 +0100 | [diff] [blame] | 101 | create the Process object with ``stdin=PIPE``. Similarly, to get anything |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 102 | other than ``None`` in the result tuple, you need to give ``stdout=PIPE`` |
| 103 | and/or ``stderr=PIPE`` too. |
| 104 | |
| 105 | .. note:: |
| 106 | |
| 107 | The data read is buffered in memory, so do not use this method if the |
| 108 | data size is large or unlimited. |
| 109 | |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 110 | .. method:: kill() |
| 111 | |
| 112 | Kills the child. On Posix OSs the function sends :py:data:`SIGKILL` to |
| 113 | the child. On Windows :meth:`kill` is an alias for :meth:`terminate`. |
| 114 | |
Brian Curtin | a1afeec | 2014-02-08 18:36:14 -0600 | [diff] [blame] | 115 | .. method:: send_signal(signal) |
Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 116 | |
| 117 | Sends the signal *signal* to the child process. |
| 118 | |
| 119 | .. note:: |
| 120 | |
| 121 | On Windows, :py:data:`SIGTERM` is an alias for :meth:`terminate`. |
| 122 | ``CTRL_C_EVENT`` and ``CTRL_BREAK_EVENT`` can be sent to processes |
| 123 | started with a *creationflags* parameter which includes |
| 124 | ``CREATE_NEW_PROCESS_GROUP``. |
| 125 | |
| 126 | .. method:: terminate() |
| 127 | |
| 128 | Stop the child. On Posix OSs the method sends :py:data:`signal.SIGTERM` |
| 129 | to the child. On Windows the Win32 API function |
| 130 | :c:func:`TerminateProcess` is called to stop the child. |
| 131 | |
| 132 | .. method:: wait(self): |
| 133 | |
| 134 | Wait for child process to terminate. Set and return :attr:`returncode` |
| 135 | attribute. |
| 136 | |
Victor Stinner | e48d4db | 2014-02-03 23:26:28 +0100 | [diff] [blame] | 137 | |
| 138 | Example |
| 139 | ------- |
| 140 | |
| 141 | Implement a function similar to :func:`subprocess.getstatusoutput`, except that |
| 142 | it does not use a shell. Get the output of the "python -m platform" command and |
| 143 | display the output:: |
| 144 | |
| 145 | import asyncio |
| 146 | import sys |
| 147 | from asyncio import subprocess |
| 148 | |
| 149 | @asyncio.coroutine |
| 150 | def getstatusoutput(*args): |
| 151 | proc = yield from asyncio.create_subprocess_exec( |
| 152 | *args, |
| 153 | stdout=subprocess.PIPE, |
| 154 | stderr=subprocess.STDOUT) |
| 155 | try: |
| 156 | stdout, _ = yield from proc.communicate() |
| 157 | except: |
| 158 | proc.kill() |
| 159 | yield from proc.wait() |
| 160 | raise |
| 161 | exitcode = yield from proc.wait() |
| 162 | return (exitcode, stdout) |
| 163 | |
| 164 | loop = asyncio.get_event_loop() |
| 165 | coro = getstatusoutput(sys.executable, '-m', 'platform') |
| 166 | exitcode, stdout = loop.run_until_complete(coro) |
| 167 | if not exitcode: |
| 168 | stdout = stdout.decode('ascii').rstrip() |
| 169 | print("Platform: %s" % stdout) |
| 170 | else: |
| 171 | print("Python failed with exit code %s:" % exitcode) |
| 172 | sys.stdout.flush() |
| 173 | sys.stdout.buffer.flush() |
| 174 | sys.stdout.buffer.write(stdout) |
| 175 | sys.stdout.buffer.flush() |
| 176 | loop.close() |