subprocess Module can generate sub processes , Connected to their input / Output / Wrong pipeline , And get their return codes .
Through this module , Can achieve python Script execution shell command .
The common usage is subprocess.run(),subprocess.Popen() It can provide more advanced features , Below to Popen For example .
class subprocess.Popen(args, bufsize=- 1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, group=None, extra_groups=None, user=None, umask=- 1, encoding=None, errors=None, text=None, pipesize=- 1)
args Is a program parameter , You can use a sequence of parameters , It could be a string . By default , If args It's a sequence , The procedure to be executed is args First of all .
Parameter sequence format :
subprocess.Popen(['ls', '-l'], shell=True)
String format :
subprocess.Popen("ls -l", shell=True)
shell Parameters determine whether to use shell To execute , if shell=True,shell By default /bin/sh, Want to use /bin/bash To carry out orders , You need to specify the executable=‘/bin/bash’
To better see in python Call in subprocess.Popen() Detailed process , Use the following code as an example :
subprocess_test.py
import time
import subprocess
s = subprocess.Popen(["sleep 50"], shell=True)
print("start")
print(s.pid)
time.sleep(20)
s.kill()
print("finish")
function subprocess_test.py, Output is as follows :
start
2474
finish
stay finish Before , perform ps -auxf > log.txt, Check the detailed implementation
log.txt
terry 5573 0.0 0.0 24368 6440 pts/3 Ss Jun25 0:00 \_ -bash
terry 2471 0.0 0.0 29112 9632 pts/3 T 13:40 0:00 \_ python3.8 subprocess_test.py
terry 2474 0.0 0.0 4636 860 pts/3 T 13:40 0:00 | \_ /bin/sh -c sleep 50
terry 2475 0.0 0.0 7932 880 pts/3 T 13:40 0:00 | \_ sleep 50
terry 3870 0.0 0.0 40100 3568 pts/3 R+ 13:40 0:00 \_ ps -auxf
Can see ,python The program does not run directly sleep 50, In fact, first derived /bin/sh Subprocesses , Again by sh perform sleep command ; And printed pid The number is exactly /bin/sh Of pid.
Popen It also provides communicate Function to interact with the derived subprocess .
Popen Options can control stdin、stdout、stderr Output mode of ,stdin、stdout、stderr Once assigned subprocess.PIPE, It means that a pipeline connecting the standard flow needs to be established , It can be downloaded from pipe Read stream in .
Popen.communicate(input=None, timeout=None)
input Option to send data to child processes ,input The format of data can be string perhaps bytes.
communicate The return value of (stdout_data, stderr_data) Tuples .
Examples of use :
res = subprocess.Popen('ls -l',stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,executable='/bin/bash')
stdout, stderr = res.communicate()
if stdout != None:
print(stdout)
if res.returncode:
print(stderr)
sys.exit(int(res.returncode))
https://docs.python.org/3/library/subprocess.html
https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess