Python subprocess module summary

  • 2020-04-02 14:19:42
  • OfStack

Subprocess is intended to replace several other older modules or functions, such as os.system os.spawn* os.popen* popen2.* commands
The simplest use of subprocess is to invoke the shell command, but you can also invoke programs and interact with them through stdout,stdin, and stderr.

The main class subprocess


subprocess.Popen(
      args,
      bufsize=0,
      executable=None,
      stdin=None,
      stdout=None,
      stderr=None,
      preexec_fn=None,
      close_fds=False,
      shell=False,
      cwd=None,
      env=None,
      universal_newlines=False,
      startupinfo=None,
      creationflags=0)

1), the args This can be a string or sequence type (such as list, tuple) that specifies the executable of the process and its parameters. If it is a sequence type, the first element is usually the path to the executable. We can also explicitly specify the path of the executable using the executeable parameter.

2), the bufsize: Specify the buffer. 0 no buffer,1 line buffer, other buffer size, negative system buffer (full buffer)

3) stdin, stdout, stderr Represents the standard input, output, and error handles of the program, respectively. They can be pipes, file descriptors or file objects, or they can be set to None to represent inheritance from the parent process.

4), preexec_fn Available only on Unix platforms, it specifies an executable object (callable object) that will be called before the child process runs.

5), Close_sfs: Under the Windows platform, if close_fds is set to True, the newly created child process will not inherit the input, output, and error pipes of the parent process. We cannot set close_fds to True and redirect the child process's standard input, output, and errors (stdin, stdout, stderr).

6) and the shell Set to true and the program will execute through the shell.

7), CWD To set the current directory of the child process

8), env Is a dictionary type that specifies the environment variables for the child process. If env = None, the child process's environment variables are inherited from the parent process.
Universal_newlines: the newline characters of text are different for different operating systems. For example, '/r/n' for Windows, '/n' for Linux. If this parameter is set to True, Python treats these newline characters as '/n'. Startupinfo and createionflags work only on Windows, and they are passed to the underlying CreateProcess() function to set some properties of the child process, such as the appearance of the main window, the priority of the process, and so on.

9), startupinfo and createionflags are only valid under Windows They are passed to the underlying CreateProcess() function to set some properties of the child process, such as the appearance of the main window, the priority of the process, and so on.

Popen method

1), Popen. Poll () : Used to check if the child process has ended. Set and return the returncode property.

2) and Popen. Wait () : Wait for the child process to finish. Set and return the returncode property.

(3), Popen.com municate input = None) : Interact with child processes. Send data to stdin or read data from stdout and stderr. Optional parameter input specifies the parameters to be sent to the child process. Communicate() returns a tuple :(stdoutdata, stderrdata). Note: if you want to send data to a process through its stdin, the parameter stdin must be set to PIPE when creating the Popen object. Similarly, if you want to get data from stdout and stderr, you must set stdout and stderr to PIPE.

4) and Popen. Send_signal (signal) : Sends a signal to the child process.

5), Popen. The terminate () : Stop child processes. On the Windows platform, this method calls the Windows API TerminateProcess () to end the child process.

6) and Popen. Kill () : Kill the child process.

7) and Popen. Stdin: If the parameter stdin is set to PIPE when creating a Popen object, popen.stdin returns a file object for the child process to send instructions. Otherwise return None.

8), Popen. Stdout: If the parameter stdout is set to PIPE when creating a Popen object, popen.stdout will return a file object for the child process to send instructions to. Otherwise return None.

9), Popen. Stderr: If the parameter stdout is set to PIPE when creating a Popen object, popen.stdout will return a file object for the child process to send instructions to. Otherwise return None.

10), Popen. Pid: Gets the process ID of the child process.

11), Popen. Returncode: Gets the return value of the process. If the process has not finished, return None.

12) subprocess.call(*popenargs, **kwargs) : Run the command. This function waits until the child process runs and returns the process's returncode. The example at the beginning of the article demonstrates the call function. If the child process does not need to interact, it can be created using this function.

13) subprocess.check_call(*popenargs, **kwargs) : As with subprocess.call(*popenargs, **kwargs), except that if the child process returns a returncode that is not 0, the CalledProcessError exception is triggered. In the exception object, include the process's returncode information.

I copied all of that

Run another program or shell in a program

I could write it like this


subprocess.Popen(' The script /shell', shell=True)

You can do that


subprocess.call(' The script /shell', shell=True)

The difference between the two is that the former is non-blocking and runs in parallel with the main program, which must wait for the command to finish executing, which is what you can do if you want the former to block

s = subprocess.Popen(' The script /shell', shell=True)
s.wait()

The program returns the result of the run

Sometimes we need the return result of the program, so we can do that.


>>> s = subprocess.Popen('ls -l', shell=True, stdout=subprocess.PIPE)
>>> s.communicate()
('xe6x80xbbxe7x94xa8xe9x87x8f 152n-rw------- 1 limbo limbo   808  7xe6x9cx88  6 17:46 0000-00-00-welcome-to-jekyll.markdown.erbndrwx------ 2 limbo limbo  4096  8xe6x9cx88 15 18:43 argndrwx------ 2 limbo limbo  4096  8xe6x9cx88  7 17:37 argvndrwxrwxr-x 2 limbo limbo  4096  9xe6x9cx88 10 15:27 cndrwxrwxr-x 3 limbo limbo  4096  9xe6x9cx88 11 14:35 d3ndrwxrwxr-x 3 limbo limbo  4096  9xe6x9n', None)

It returns a tuple :(stdoutdata, stderrdata)

There is another, simpler method of subprocess that returns stdout with the same effect


>>> s = subprocess.check_output('ls -l', shell=True)
>>> s
'xe6x80xbbxe7x94xa8xe9x87x8f 152n-rw------- 1 limbo limbo   808  7xe6x9cx88  6 17:46 0000-00-00-welcome-to-jekyll.markdown.erbndrwx------ 2 limbo limbo  4096  8xe6x9cx88 15 18:43 argndrwx------ 2 limbo limbo  4096  8xe6x9cx88  7 17:37 argvndrwxrwxr-x 2 limbo limbo  4096  9xe6x9cx88 10 15:27 cndrwxrwxr-x 3 limbo limbo  4096  9xe6x9cx88 11 14:35 d3ndrwxrwxr-x 3 limbo limbo  4096  9xe6x9n'

The former enables more interactions, such as stderr and stdin, but implements the definition of Popen(stdin= subprocess.pipe, stderr=subprocess) when Popen is invoked earlier

Input to the child process


import subprocess
child = subprocess.Popen(["cat"], stdin=subprocess.PIPE)
child.communicate("vamei")

() is not empty, then write subprocess.pipe, is empty, then read from subprocess.pipe


Subprocess. The PIPE


#!/usr/bin/env python
import subprocess
child1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE)
child2 = subprocess.Popen(["wc"], stdin=child1.stdout,stdout=subprocess.PIPE)
out = child2.communicate()
print out

This is actually the process


child1.stdout-->subprocess.PIPE child2.stdin<--subprocess.PIPE child2.stdout-->subprocess.PIPE

Note that communicate() is a method of the Popen object that blocks the parent process until the child process completes.

Subprocess.pipe actually provides a cache for the text stream. Note that the communicate() method is a method of the Popen object that blocks the parent process until the child process completes.


Related articles: