Victor Stinner | 0844438 | 2014-02-02 22:43:39 +0100 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
| 2 | |
| 3 | Subprocess |
| 4 | ========== |
| 5 | |
| 6 | Create a subproces |
| 7 | ------------------ |
| 8 | |
| 9 | .. function:: create_subprocess_shell(cmd, stdin=None, stdout=None, stderr=None, loop=None, limit=None, \*\*kwds) |
| 10 | |
| 11 | Run the shell command *cmd* (:class:`str`)`. Return a :class:`Process` |
| 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 | |
| 60 | .. attribute:: stdin |
| 61 | |
| 62 | Standard input stream (write), ``None`` if the process was created with |
| 63 | ``stdin=None``. |
| 64 | |
| 65 | .. attribute:: stdout |
| 66 | |
| 67 | Standard output stream (read), ``None`` if the process was created with |
| 68 | ``stdout=None``. |
| 69 | |
| 70 | .. attribute:: stderr |
| 71 | |
| 72 | Standard error stream (read), ``None`` if the process was created with |
| 73 | ``stderr=None``. |
| 74 | |
| 75 | .. attribute:: pid |
| 76 | |
| 77 | The identifier of the process. |
| 78 | |
| 79 | Note that if you set the *shell* argument to ``True``, this is the |
| 80 | process identifier of the spawned shell. |
| 81 | |
| 82 | .. attribute:: returncode |
| 83 | |
| 84 | Return code of the process when it exited. A ``None`` value indicates |
| 85 | that the process has not terminated yet. |
| 86 | |
| 87 | A negative value ``-N`` indicates that the child was terminated by signal |
| 88 | ``N`` (Unix only). |
| 89 | |
| 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 |
| 101 | create the Popen object with ``stdin=PIPE``. Similarly, to get anything |
| 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 | |
| 110 | .. method:: get_subprocess() |
| 111 | |
| 112 | Get the underlying :class:`subprocess.Popen` object. |
| 113 | |
| 114 | .. method:: kill() |
| 115 | |
| 116 | Kills the child. On Posix OSs the function sends :py:data:`SIGKILL` to |
| 117 | the child. On Windows :meth:`kill` is an alias for :meth:`terminate`. |
| 118 | |
| 119 | .. method:: send_signal(signale) |
| 120 | |
| 121 | Sends the signal *signal* to the child process. |
| 122 | |
| 123 | .. note:: |
| 124 | |
| 125 | On Windows, :py:data:`SIGTERM` is an alias for :meth:`terminate`. |
| 126 | ``CTRL_C_EVENT`` and ``CTRL_BREAK_EVENT`` can be sent to processes |
| 127 | started with a *creationflags* parameter which includes |
| 128 | ``CREATE_NEW_PROCESS_GROUP``. |
| 129 | |
| 130 | .. method:: terminate() |
| 131 | |
| 132 | Stop the child. On Posix OSs the method sends :py:data:`signal.SIGTERM` |
| 133 | to the child. On Windows the Win32 API function |
| 134 | :c:func:`TerminateProcess` is called to stop the child. |
| 135 | |
| 136 | .. method:: wait(self): |
| 137 | |
| 138 | Wait for child process to terminate. Set and return :attr:`returncode` |
| 139 | attribute. |
| 140 | |