class asyncio.subprocess.Process
A subprocess created by the create_subprocess_exec()
or the create_subprocess_shell()
function.
The API of the Process
class was designed to be close to the API of the subprocess.Popen
class, but there are some differences:
- There is no explicit
poll()
method - The
communicate()
andwait()
methods don’t take a timeout parameter: use thewait_for()
function - The universal_newlines parameter is not supported (only bytes strings are supported)
- The
wait()
method of theProcess
class is asynchronous whereas thewait()
method of thePopen
class is implemented as a busy loop.
This class is not thread safe. See also the Subprocess and threads section.
-
coroutine wait()
-
Wait for child process to terminate. Set and return
returncode
attribute.This method is a coroutine.
Note
This will deadlock when using
stdout=PIPE
orstderr=PIPE
and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use thecommunicate()
method when using pipes to avoid that.
-
coroutine communicate(input=None)
-
Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be data to be sent to the child process, or
None
, if no data should be sent to the child. The type of input must be bytes.communicate()
returns a tuple(stdout_data, stderr_data)
.If a
BrokenPipeError
orConnectionResetError
exception is raised when writing input into stdin, the exception is ignored. It occurs when the process exits before all data are written into stdin.Note that if you want to send data to the process’s stdin, you need to create the Process object with
stdin=PIPE
. Similarly, to get anything other thanNone
in the result tuple, you need to givestdout=PIPE
and/orstderr=PIPE
too.This method is a coroutine.
Note
The data read is buffered in memory, so do not use this method if the data size is large or unlimited.
Changed in version 3.4.2: The method now ignores
BrokenPipeError
andConnectionResetError
.
-
send_signal(signal)
-
Sends the signal signal to the child process.
Note
On Windows,
SIGTERM
is an alias forterminate()
.CTRL_C_EVENT
andCTRL_BREAK_EVENT
can be sent to processes started with a creationflags parameter which includesCREATE_NEW_PROCESS_GROUP
.
-
terminate()
-
Stop the child. On Posix OSs the method sends
signal.SIGTERM
to the child. On Windows the Win32 API functionTerminateProcess()
is called to stop the child.
-
kill()
-
Kills the child. On Posix OSs the function sends
SIGKILL
to the child. On Windowskill()
is an alias forterminate()
.
-
stdin
-
Standard input stream (
StreamWriter
),None
if the process was created withstdin=None
.
-
stdout
-
Standard output stream (
StreamReader
),None
if the process was created withstdout=None
.
-
stderr
-
Standard error stream (
StreamReader
),None
if the process was created withstderr=None
.
Warning
Use the communicate()
method rather than .stdin.write
, .stdout.read
or .stderr.read
to avoid deadlocks due to streams pausing reading or writing and blocking the child process.
-
pid
-
The identifier of the process.
Note that for processes created by the
create_subprocess_shell()
function, this attribute is the process identifier of the spawned shell.
-
returncode
-
Return code of the process when it exited. A
None
value indicates that the process has not terminated yet.A negative value
-N
indicates that the child was terminated by signalN
(Unix only).
Please login to continue.