Python subprocess module learning summary

  • 2020-04-02 13:32:16
  • OfStack

I. subprocess and commonly used encapsulation functions
When we run python, we all create and run a process. Like a Linux process, a process can fork a child process and have that child exec another program. In Python, we fork a child process through the subprocess package in the standard library and run an external program.
The subprocess package defines several functions that create child processes, each of which creates the child process in a different way, so we can use one of these as needed. In addition, subprocess provides tools for managing standard streams and pipes to use text communication between processes.

Subprocess. The call ()
The parent waits for the child to complete
Return exit information (returncode, equivalent to Linux exit code)

Subprocess. Check_call ()
The parent waits for the child to complete
Return 0
Check out information, unless returncode to 0, the name error subprocess. CalledProcessError, this object contains a returncode attribute, usable and try... Except... To check the

Subprocess. Check_output ()
The parent waits for the child to complete
Returns the output of the child process to standard output
Check out information, unless returncode to 0, the name error subprocess. CalledProcessError, this object contains a returncode attribute and the output properties, the output properties for the output results of the standard output available to try... Except... To check.

These three functions are used in a similar way, as illustrated by subprocess.call() :


>>> import subprocess
>>> retcode = subprocess.call(["ls", "-l"])
# and shell In the command ls -a The result is the same 
>>> print retcode
0

Pass the program name (ls) along with the argument (-l) in a table to subprocess.call()

The shell defaults to False. In Linux, when shell=False, Popen calls os.execvp() to execute the program specified by args. When shell=True, if args is a string, Popen directly calls the shell of the system to execute the program specified by args.

The above example can also be written as follows:

>>> retcode = subprocess.call("ls -l",shell=True)

Under Windows, Popen calls CreateProcess() to execute the external program specified by args, regardless of the shell's value. If args is a sequence, list2cmdline() is first converted to a string, but it is important to note that not all programs under MS Windows can use list2cmdline to convert to a command line string.

Subprocess. Popen ()


class 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)

In fact, the above functions are all wrapper based on Popen(). The purpose of these packages is to make it easy for us to use child processes. When we want to personalize our requirements, we turn to the Popen class, which generates objects that represent child processes.

Unlike the above encapsulation, after the Popen object is created, the main program does not automatically wait for the child process to complete. We must call the object's wait() method before the parent process waits (that is, blocks). For example:

>>> import subprocess
>>> child = subprocess.Popen(['ping','-c','4','blog.linuxeye.com'])
>>> print 'parent process'

As you can see from the results of the run, the parent process does not wait for the child to complete after it starts, but runs print directly.

Compare the situation of waiting:

>>> import subprocess
>>> child = subprocess.Popen('ping -c4 blog.linuxeye.com',shell=True)
>>> child.wait()
>>> print 'parent process'

As you can see from the results of the run, the parent process starts the child process and waits for the child to complete before running print.
In addition, you can do other things to the child in the parent process, such as the child object in our example above:

child.poll() #  Check the status of the child process 
child.kill() #  Terminate child process 
child.send_signal() #  Sends a signal to the child process 
child.terminate() #  Terminate child process 

The child process's PID is stored in child.pid
Second, text flow control of child process
The following attributes of standard input, standard output and standard error of the child process are respectively represented:

child.stdin
child.stdout
child.stderr

Standard input, standard output, and standard error can be changed when Popen() creates a child process, and subprocess.pipe can be used to connect the input and output of multiple child processes to form a PIPE, as shown in the following two examples:
>>> import subprocess
>>> child1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE)
>>> print child1.stdout.read(),
# or child1.communicate()
>>> import subprocess
>>> child1 = subprocess.Popen(["cat","/etc/passwd"], stdout=subprocess.PIPE)
>>> child2 = subprocess.Popen(["grep","0:0"],stdin=child1.stdout, stdout=subprocess.PIPE)
>>> out = child2.communicate()

Subprocess.pipe actually provides a cache for the text stream. The stdout of child1 outputs the text to the cache, and then the stdin of child2 reads the text from the PIPE. The output text of child2 is also stored in a PIPE until the communicate() method reads the text in the PIPE from the PIPE.
Note: communicate() is a method of a Popen object that blocks the parent process until the child process completes


Related articles: