Subprocess module provides inter-process operation
Call method creates a child process
Retcode=subprocess.call (["ls","- L"," -A"]) Results:-RW-------1 root root 1570 Feb 3 anaconda-KS.CFGDRWXR-xr-x 2 root root 4096 Feb 3 2015Desktop-rw-r--r--1 root root 59038 Feb 3 2015Install.log-rw-r--r--1 root root 5266 Feb 3 2015Install.log.syslog-rw-r--r--1 root root 908984 may 12:29 nginx-1.9.15. tar.gz-rw-r--r--1 root root 2053336 may 18:12 pcre-8.38. tar.gz-rw-r--r--1 root root 18207283 may 18:13 php-7.0.6. tar.gz-rw-r--r--1 root root 148960 may 09:42 zabbix-2.2.1-1. el5.x86_64.rpm-rw-r--r--1 root root 248051 may 09:41 zabbix-agent-2.2.1-1. el5.x86_64.rpm-rw-r--r--1 root root 11296 may 09:33 zabbix-release-2.4-1.el6.noarch.rpm
Under Linux, when Shell=false, Popen calls OS.EXECVP () to execute the program specified by args, and shell=true, if Args is a string, Popen calls the system's shell directly to execute the program specified by args. If args is a sequence, the first item of args is the definition of the program command string, and other items are additional parameters when invoking the system shell.
>>> Retcode=subprocess.call ("ls-l", shell=True) Total21244-RW-------1 root root 1570 Feb 3 anaconda-KS.CFGDRWXR-xr-x 2 root root 4096 Feb 3 2015Desktop-rw-r--r--1 root root 59038 Feb 3 2015Install.log-rw-r--r--1 root root 5266 Feb 3 2015Install.log.syslog-rw-r--r--1 root root 908984 may 12:29 nginx-1.9.15. tar.gz-rw-r--r--1 root root 2053336 may 18:12 pcre-8.38. tar.gz-rw-r--r--1 root root 18207283 may 18:13 php-7.0.6. tar.gz-rw-r--r--1 root root 148960 may 09:42 zabbix-2.2.1-1. el5.x86_64.rpm-rw-r--r--1 root root 248051 may 09:41 zabbix-agent-2.2.1-1. el5.x86_64.rpm-rw-r--r--1 root root 11296 may 09:33 zabbix-release-2.4-1.el6.noarch.rpm
In fact, several of the above functions are based on the Popen () package (wrapper). The purpose of these packages is to make it easy for us to use child processes. When we want to personalize our needs more, we turn to the Popen class, which produces objects that represent child processes.
Unlike the package above, the main program does not automatically wait for the child process to complete after the Popen object is created. We must call the object's Wait () method, and the parent process waits (that is, block blocks), for example:
Import subprocess
Child = subprocess. Popen ([' Ping ', '-N ', ' 4 ', ' blog.linuxeye.com ')
# child.wait ()
Print (' parent process ')
Results:
As seen from the results of the run, the parent process does not wait for the child to complete after it has been opened, but runs print directly.
Parent process
ImportSubprocesschild= subprocess. Popen (['Ping','- N','4','blog.linuxeye.com']) child.wait ()Print('Parent Process')
Result: (Wait for the child process to complete) is pinging blog.linuxeye.com [121.40.145.83] has 32bytes of data: from121.40.145.83 's reply: Byte =32 time =29ms ttl=52from121.40.145.83 's reply: Byte =32 time =33ms ttl=52from121.40.145.83 's reply: Byte =32 time =30ms ttl=52from121.40.145.83 's reply: Byte =32 time =32ms ttl=52121.40.145.83Ping Statistics: packets: Sent= 4, Received = 4, missing = 0 (0%lost), estimated time to round trip (in milliseconds): Shortest= 29ms, maximum = 33ms, average =31msparent Process
You can also do other things to the child process while the parent process is waiting
# Check child process status # terminating a child process # send a signal to a child process # terminating a child process
The standard input, standard output, and standard errors for child processes are represented by the following attributes:
Child.stdinchild.stdoutchild.stderr
You can change standard input, standard output, and standard errors when Popen () is established, and can take advantage of subprocess. Pipes connect the inputs and outputs of multiple sub-processes together to form a pipe, as in the following 2 examples:
Put the output in the stdout and read it from the stdout.
>>> child1 = subprocess. Popen (["ls","- L"], stdout=subprocess. PIPE)>>>PrintChild1.stdout.read () Total21244-RW-------1 root root 1570 Feb 3 anaconda-KS.CFGDRWXR-xr-x 2 root root 4096 Feb 3 2015Desktop-rw-r--r--1 root root 59038 Feb 3 2015Install.log-rw-r--r--1 root root 5266 Feb 3 2015Install.log.syslog-rw-r--r--1 root root 908984 may 12:29 nginx-1.9.15. tar.gz-rw-r--r--1 root root 2053336 may 18:12 pcre-8.38. tar.gz-rw-r--r--1 root root 18207283 may 18:13 php-7.0.6. tar.gz-rw-r--r--1 root root 148960 may 09:42 zabbix-2.2.1-1. el5.x86_64.rpm-rw-r--r--1 root root 248051 may 09:41 zabbix-agent-2.2.1-1. el5.x86_64.rpm-rw-r--r--1 root root 11296 may 09:33 zabbix-release-2.4-1.el6.noarch.rpm
>>>Importsubprocess>>> child1 = subprocess. Popen (["Cat","/etc/passwd"], stdout=subprocess. PIPE)>>> child2 = subprocess. Popen (["grep","0:0"],stdin=child1.stdout, stdout=subprocess. PIPE)>>> out =child2.communicate ()>>>PrintOut ('root:x:0:0:root:/root:/bin/bash\n', None)
>>> Import subprocess
>>> child1 = subprocess. Popen (["Cat", "/etc/passwd"], stdout=subprocess. PIPE)
>>> child2 = subprocess. Popen (["grep", "0:0"],stdin=child1.stdout, stdout=subprocess. PIPE)
>>> Print Child2.stdout.read
<built-in method Read of file object at 0x2b5838884be8>
>>> Print Child2.stdout.read ()
Root:x:0:0:root:/root:/bin/bash
Python subprocess Module