Detailed introduction to the subprocess package of the Python standard library

高洛峰
Release: 2017-03-23 16:51:12
Original
2254 people have browsed it

The content here is based on Linux process foundation and Linux text stream. The main function of the subprocess package is to execute external commands and programs. For example, I need to use wget to download a file. I'm calling the wget program in Python. In this sense, subprocess functions similarly to the shell.

subprocess and commonly used encapsulation functions

When we run python, we are creating and running a process. As we introduced in Linux Process Basics, a process can fork a child process and let the child process exec another program. In Python, we use the subprocess package in the standard library to fork a subprocess and run an external program (fork, exec, see Linux Process Basics).

The subprocess package defines several functions for creating subprocesses. These functions create subprocesses in different ways, so we can choose one of them to use according to our needs. In addition, subprocess also provides some tools for managing standard streams and pipes to use text communication between processes.

When using the functions in the subprocess package to create a child process, please note:

1) After creating the child process, whether the parent process is paused and waits for the child process to run.

2) What does the function return?

3) When the returncode is not 0, how does the parent process handle it.

subprocess.call()
Copy after login



The parent process waits for the child process to complete

Return exit information (returncode, equivalent to exit code, see Linux process basics)

subprocess.check_call()
Copy after login


The parent process waits for the child process to complete

Return 0

Check the exit information. If the returncode is not 0, raise the error subprocess.CalledProcessError. This object contains the returncode Attributes can be checked with try...except... (see Python error handling).

subprocess.check_output()
Copy after login



The parent process waits for the child process to complete

Return the output results of the child process to standard output

Check the exit information, if the returncode does not If it is 0, the error subprocess.CalledProcessError is raised. This object contains the returncode attribute and the output attribute. The output attribute is the output result of the standard output and can be checked by try...except....

The usage of these three functions is similar. We use subprocess.call() to illustrate:

import subprocess
rc = subprocess.call(["ls","-l"])
Copy after login



We will combine the program name (ls) and The parameters (-l) are put together in a table and passed to subprocess.call()

An entire string can be interpreted through a shell:

import subprocess
out = subprocess.call("ls -l", shell=True)
out = subprocess.call("cd ..", shell=True)
Copy after login



We used the shell=True parameter. This time, we use an entire string instead of a table to run the child process. Python will first run a shell and then use this shell to interpret the entire string.

Some of the shell commands are built-in commands of the shell. These commands must be run through the shell, $cd. shell=True allows us to run such commands.

Popen()

In fact, our three functions above are all wrappers based on Popen(). The purpose of these encapsulation is to make it easier for us to use subprocesses. When we want to be more customized to our needs, we turn to the Popen class, which generates objects that represent child processes.

Different from the above encapsulation, after the Popen object is created, the main program will not automatically wait for the sub-process to complete. We must call the wait() method of the object before the parent process will wait (that is, block the block):

import subprocess
child = subprocess.Popen(["ping","-c","5","www.google.com"])
print("parent process")
Copy after login



We can see from the running results that the parent process is opening the child The process does not wait for the child to complete, but directly runs print.

Compare the waiting situation:

import subprocess
child = subprocess.Popen(["ping","-c","5","www.google.com"])
child.wait()
print("parent process")
Copy after login



In addition, you can also perform other operations on the child process in the parent process, such as the child in our example above Object:

child.poll()      # 检查子进程状态

child.kill()      # 终止子进程

child.send_signal()  # 向子进程发送信号

child.terminate()   # 终止子进程
Copy after login


The PID of the child process is stored in child.pid

The text flow control of the child process

(Inherited child child process) The standard input, standard output and standard error of the child process can also be represented by the following attributes:

child.stdin

child.stdout

child.stderr

We can change the standard input, standard output and standard error when Popen() creates a subprocess, and can use subprocess.PIPE to connect the input and output of multiple subprocesses together to form a pipe:

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)
Copy after login


subprocess.PIPE actually provides a buffer area for the text stream. child1's stdout outputs the text to the buffer area, and then child2's stdin reads the text from the PIPE. The output text of child2 is also stored in PIPE until the communicate() method reads the text in PIPE from PIPE.

It should be noted that communicate() is a method of the Popen object, which blocks the parent process until the child process is completed.

We can also use the communicate() method to use PIPE to input input to the child process:

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


我们启动子进程之后,cat会等待输入,直到我们用communicate()输入"vamei"。

通过使用subprocess包,我们可以运行外部程序。这极大的拓展了Python的功能。如果你已经了解了操作系统的某些应用,你可以从Python中直接调用该应用(而不是完全依赖Python),并将应用的结果输出给Python,并让Python继续处理。shell的功能(比如利用文本流连接各个应用),就可以在Python中实现。

总结

subprocess.call, subprocess.check_call(), subprocess.check_output()

subprocess.Popen(), subprocess.PIPE

Popen.wait(), Popen.communicate()


The above is the detailed content of Detailed introduction to the subprocess package of the Python standard library. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template