python subprocess kills all derived child process methods

  • 2020-05-19 05:12:26
  • OfStack

Here's what we learned this afternoon.

Release system need to respond to the user's interrupt request, need in GET method to kill the child process by subprocess derived -- beginning with os directly. kill found the child the child cannot kill, Google has some 1, found kill can kill process group, so the test, but by default, subprocess derived process group and the main program, which is my web py process is in a process group, if this kill, that is adjustable.

If you go to google, and you look at document of subprocess, you find this variable:

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 was so happy that I thought I could solve the problem. After a long test, I realized that it was only windows. I went there, but I thought that win could do it, and linux could also do it

preexec_fn

Once again, it is an google, isn't it an object? I got an setpgid(0,0) test. The sub-process still belongs to the same process group as the main process.

preexec_fn = os.setpgrp

This actually solves the problem of creating a new process group.

Continue to work hard, and you will encounter the problem of zombie process. os.waitpid will be solved after 1 click.

At the beginning of waitpid, I spent a long time on man on linxu. I was still worried when I looked at the parameters in linxu manual. However, os and waitpid in python had no parameters and no return value. But it just solved my problem.

Here's today's full test code


[liufeng@1.2.3.4 kill-subprocess]$ cat sub-process.py 
import subprocess
import os
import time

def my_func():

# Derive two child processes, and several within them sleep The grandson process is primarily for testing purposes kill Process group. 

run_str2 = '/bin/sh test.sh'
run_str = '/bin/sh test_quick.sh'
cmd2 = run_str.split()
cmd = run_str.split()

# To test the 1 Jo preexec_fn It turns out that it works, right python I still don't understand the concept of the object, newbie, 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.pid
pgid = os.getpgid(pid)
print "pid: %d\n" %pid
print "pgid: %d\n" %pgid
return pid


pid = my_func()
#p.wait()
print "now , sleep 2s ,then , os.kill gpid %d" % pid
time.sleep(20)


a = os.kill(-pid, 9)
print "kill,return:"
print a


# kill I tested it kill  unauthorized root Process, will report an error: permissions are not allowed 
#  To test the kill p p2  Can be 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 a
time.sleep(22)


print "done..."


#p.terminate()
#p.kill()
#p.wait()
#
#time.sleep(40)
#os.kill(pid, 9)

Related articles: