Recently, a small tool needs to use the python word process. After reading the python API, the subprocess module can be implemented, and it is quite simple.
To implement a simple self-process, you only need
Import sys, OS, subprocess <br/> P = subprocess. popen (["ls", "-l"], shell = false, stdin = none, stdout = none, stderr = none) <br/> Print p. PID <br/> Print p. returncode
In this case, the PID will be the ID of the sub-process. If the sub-process is launched normally, the returncode will be none.
You can also use the followingCodeCreate a sub-process
Import sys, OS, subprocess <br/> retcode = subprocess. call (["ls", "-l"], shell = false, stdin = none, stdout = none, stderr = none) <br/> Print retcode
At this time, if the sub-process is launched normally, the retcode will be 0.
In addition, if you do not want to output the printed information of the created sub-process, you can set stdout and stderr in the method parameters to subprocess. pipe.
The following is a small example: create a sub-process P1, and then create a sub-process p2 to end P1 execution.
Import sys, OS, subprocess <br/> P1 = subprocess. popen (["Sleep", "60"], shell = false, stdin = none, stdout = none, stderr = none) <br/> Print p1.pid <br/> Print p1.returncode <br/> P2 = subprocess. popen (["kill", STR (p1.pid)], shell = false, stdin = none, stdout = none, stderr = none) <br/> Print p2.pid <br/> Print p2.returncode