asyncio.asyncio.subprocess.Process

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() and wait() methods don’t take a timeout parameter: use the wait_for() function
  • The universal_newlines parameter is not supported (only bytes strings are supported)
  • The wait() method of the Process class is asynchronous whereas the wait() method of the Popen 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 or stderr=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 the communicate() 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 or ConnectionResetError 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 than None in the result tuple, you need to give stdout=PIPE and/or stderr=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 and ConnectionResetError.

send_signal(signal)

Sends the signal signal to the child process.

Note

On Windows, SIGTERM is an alias for terminate(). CTRL_C_EVENT and CTRL_BREAK_EVENT can be sent to processes started with a creationflags parameter which includes CREATE_NEW_PROCESS_GROUP.

terminate()

Stop the child. On Posix OSs the method sends signal.SIGTERM to the child. On Windows the Win32 API function TerminateProcess() is called to stop the child.

kill()

Kills the child. On Posix OSs the function sends SIGKILL to the child. On Windows kill() is an alias for terminate().

stdin

Standard input stream (StreamWriter), None if the process was created with stdin=None.

stdout

Standard output stream (StreamReader), None if the process was created with stdout=None.

stderr

Standard error stream (StreamReader), None if the process was created with stderr=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 signal N (Unix only).

doc_python
2016-10-07 17:26:37
Comments
Leave a Comment

Please login to continue.