blob: 3d176f100dff1a328c258acf6dfe378cf6e60d74 [file] [log] [blame]
Victor Stinner08444382014-02-02 22:43:39 +01001.. currentmodule:: asyncio
2
3Subprocess
4==========
5
6Create a subproces
7------------------
8
9.. function:: create_subprocess_shell(cmd, stdin=None, stdout=None, stderr=None, loop=None, limit=None, \*\*kwds)
10
Benjamin Peterson233eac42014-02-03 14:08:00 -050011 Run the shell command *cmd* given as a string. Return a :class:`Process`
Victor Stinner08444382014-02-02 22:43:39 +010012 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
22Use 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
31Constants
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
55Process
56-------
57
58.. class:: asyncio.subprocess.Process
59
Victor Stinnerb79eb052014-02-03 23:08:14 +010060 .. 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 Stinner08444382014-02-02 22:43:39 +010075 .. 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 Stinnerb79eb052014-02-03 23:08:14 +010090 .. attribute:: subprocess
Victor Stinner08444382014-02-02 22:43:39 +010091
Victor Stinnerb79eb052014-02-03 23:08:14 +010092 Underlying :class:`subprocess.Popen` object.
Victor Stinner08444382014-02-02 22:43:39 +010093
94 .. method:: communicate(input=None)
95
96 Interact with process: Send data to stdin. Read data from stdout and
97 stderr, until end-of-file is reached. Wait for process to terminate.
98 The optional *input* argument should be data to be sent to the child
99 process, or ``None``, if no data should be sent to the child. The type
100 of *input* must be bytes.
101
102 :meth:`communicate` returns a tuple ``(stdoutdata, stderrdata)``.
103
104 Note that if you want to send data to the process's stdin, you need to
105 create the Popen object with ``stdin=PIPE``. Similarly, to get anything
106 other than ``None`` in the result tuple, you need to give ``stdout=PIPE``
107 and/or ``stderr=PIPE`` too.
108
109 .. note::
110
111 The data read is buffered in memory, so do not use this method if the
112 data size is large or unlimited.
113
Victor Stinner08444382014-02-02 22:43:39 +0100114 .. 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
Victor Stinnere48d4db2014-02-03 23:26:28 +0100141
142Example
143-------
144
145Implement a function similar to :func:`subprocess.getstatusoutput`, except that
146it does not use a shell. Get the output of the "python -m platform" command and
147display the output::
148
149 import asyncio
150 import sys
151 from asyncio import subprocess
152
153 @asyncio.coroutine
154 def getstatusoutput(*args):
155 proc = yield from asyncio.create_subprocess_exec(
156 *args,
157 stdout=subprocess.PIPE,
158 stderr=subprocess.STDOUT)
159 try:
160 stdout, _ = yield from proc.communicate()
161 except:
162 proc.kill()
163 yield from proc.wait()
164 raise
165 exitcode = yield from proc.wait()
166 return (exitcode, stdout)
167
168 loop = asyncio.get_event_loop()
169 coro = getstatusoutput(sys.executable, '-m', 'platform')
170 exitcode, stdout = loop.run_until_complete(coro)
171 if not exitcode:
172 stdout = stdout.decode('ascii').rstrip()
173 print("Platform: %s" % stdout)
174 else:
175 print("Python failed with exit code %s:" % exitcode)
176 sys.stdout.flush()
177 sys.stdout.buffer.flush()
178 sys.stdout.buffer.write(stdout)
179 sys.stdout.buffer.flush()
180 loop.close()