Here are the results of this afternoon's research.
Publishing system need to respond to the user's interrupt request, need to kill the subprocess derived from subprocess in the GET method, at first directly with the discovery of the subprocess of the subprocess can not be kill, Google some, found that the kill can kill the process group, so the test, but by default, the subprocess derived from the process group and the main program, that is, my process is in the A process group, this if kill, then the transfer of.
Continuing to dig through google, I found this variable when looking at the document for 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 () on the subprocess.
This flag is ignored if CREATE_NEW_CONSOLE is specified.
relatively happy, thought I could solve the problem, the results of the test half a day, only to understand that this thing is ONLY windows, I go ah, but thought of, win can do, linux can certainly also, so locate to the
preexec_fn
Another google, is not the object, got a setpgid(0,0) tested, the child process is still and the main process belongs to the same process group, and then a bright idea:
preexec_fn =
This surprisingly solves the problem of newly spawned process groups.
Keep up the good work, all I encountered later was a dead process problem, which was solved after a bit.
When I first started to waitpid, I also manned half a day on linxu, and looked at the parameters in the linxu manual, but I still don't feel comfortable, and it turns out that the python one doesn't have that many parameters, and there is no return value, so it's simple. But it solved my problem.
Here is today's fully tested code
[[email protected] kill-subprocess]$ cat import subprocess import os import time def my_func(): # Spawns two child processes, which in turn spawn several sleep grandchildren, mainly to test the kill process group. run_str2 = '/bin/sh ' run_str = '/bin/sh test_quick.sh' cmd2 = run_str.split() cmd = run_str.split() #Tested some values of preexec_fn and eventually found out that it works, still don't understand the concept of python's objects, newbie, newbie. #p = (cmd, stdin = , stdout = , stderr = , shell = False, creationflags = subprocess.CREATE_NEW_PROCESS_GROUP) #p = (cmd, stdin = , stdout = , stderr = , shell = False, creationflags = 0) p = (cmd, stdin = , stdout = , stderr = , shell = False, preexec_fn = ) p2 = (cmd2, stdin = , stdout = , stderr = , shell = False, preexec_fn = ) #@p = (cmd, stdin = , stdout = , stderr = , shell = False, preexec_fn = (0, 0) ) pid = pgid = (pid) print "pid: %d\n" %pid print "pgid: %d\n" %pgid return pid pid = my_func() #() print "now , sleep 2s ,then , gpid %d" % pid (20) a = (-pid, 9) print "kill,return:" print a # kill, I tested kill root process with no privileges, and it gives me an error: privileges not allowed. # Tested kill, p, p2, both kill # #a = (2445, 9) #print "kill root process 2445 ,return:" #print a #() #(pgid, 0) # 2445 is a root process #(2445, 0) #(, 0) (pid, 0) print "waitpid,return:" print a (22) print "done..." #() #() #() # #(40) #(pid, 9)
Above this python subprocess kill all the derived child process method is all that I have shared with you, I hope to give you a reference, and I hope you support me more.