Python subprocess kills all derived sub-process methods, pythonsubprocess
The following are the results of this afternoon's study.
The publishing system must respond to the user's interrupt request and kill the subprocess derived from subprocess in the GET method. kill: the sub-process of the sub-process cannot be killed. Google has found that kill can kill the process group and test it. However, by default, the subprocess-Derived Process Group and main program, that is, my web. the py process is in a process group. If it is killed, it is called.
Continue to google. This variable is found when you read the document of subprocess:
Subprocess. CREATE_NEW_PROCESS_GROUPA Popen creationflags parameter to specify that a new process group will be created. This flag is necessary for using OS. kill () on the subprocess.
This flag is ignored if CREATE_NEW_CONSOLE is specified.
I'm glad that I thought it could solve the problem. I tested the problem for half a day before I realized that it was only windows. I went there, But I thought windows would certainly do what windows can do, so locate
Preexec_fn
Another google call, not an object, made a setpgid () test, the sub-process is still in the same process group as the main process, and then a brainwave:
Preexec_fn = OS. setpgrp
In this way, the problem of forming a process group is solved.
Continue to work hard. What we will see later is the zombie process problem. The OS. waitpid will be solved after a moment.
At the beginning of waitpid, I still had a man on linxu for half a day. Looking at the parameters in the linxu manual, I still don't feel relieved. The result is the OS in python. waitpid does not have so many parameters and does not return values. But it solved my problem.
The following is the complete test code for today.
[Liufeng@1.2.3.4 kill-subprocess] $ cat sub-process.py import subprocessimport osimport timedef my_func, it is mainly used to test the kill process group. Run_str2 = '/bin/sh test. sh 'run _ str = '/bin/sh test_quick.sh' cmd2 = run_str.split () cmd = run_str.split () # tested some values of preexec_fn, and finally found that they can be used, I still don't understand the concept of python objects. I am a newbie and newbie. # P = subprocess. popen (cmd, stdin = subprocess. PIPE, stdout = subprocess. PIPE, stderr = subprocess. PIPE, shell = False, creationflags = subprocess. CREATE_NEW_PROCESS_GROUP) # p = subprocess. popen (cmd, stdin = subprocess. PIPE, stdout = subprocess. PIPE, stderr = subprocess. PIPE, shell = False, creationflags = 0) p = subprocess. popen (cmd, stdin = subprocess. PIPE, stdout = subprocess. PIPE, stderr = subprocess. PIPE, shell = False, preexec_fn = OS. setpgrp) p2 = subprocess. popen (cmd2, stdin = subprocess. PIPE, stdout = subprocess. PIPE, stderr = subprocess. PIPE, shell = False, preexec_fn = OS. setpgrp) # @ p = subprocess. popen (cmd, stdin = subprocess. PIPE, stdout = subprocess. PIPE, stderr = subprocess. PIPE, shell = False, preexec_fn = OS. setpgid (0, 0) pid = p. pidpgid = OS. getpgid (pid) print "pid: % d \ n" % pidprint "pgid: % d \ n" % pgidreturn pidpid = my_func () # p. wait () print "now, sleep 2 s, then, OS. kill gpid % d "% pidtime. sleep (20) a = OS. kill (-pid, 9) print "kill, return:" print a # kill, I tested the root process that has no permission to kill and reported an error: permission not allowed # test kill p p2 can kill # a = OS. kill (2445, 9) # print "kill root process 2445, return:" # print a # p. wait () # OS. waitpid (pgid, 0) #2445 is a root process # OS. waitpid (2445, 0) # OS. waitpid (p2.pid, 0) OS. waitpid (pid, 0) print "waitpid, return:" print atime. sleep (22) print "done... "# p. terminate () # p. kill () # p. wait () # time. sleep (40) # OS. kill (pid, 9)
The above python subprocess method to kill all the derived sub-processes is all the content that I shared with you. I hope to give you a reference, and I hope you can provide more support for the customer's house.