Python subprocess kill all derived sub-processes

Source: Internet
Author: User
Tags python subprocess

Here are the results of this afternoon's research. the publishing system needs to respond to the user's interrupt request, need to kill the child process derived by subprocess in the Get method, just start to find the child process with Os.kill not kill, Google some, found that kill can kill the process group, then test, but by default , subprocess derived process group and main program, that is, my web.py process is in a process group, if kill, then the tune. continue to Google, look at Subprocess's document when found this variable:subprocess. Create_new_process_group

A Popen creationflags parameter to specify a new process group would be created. This flag was necessary for using Os.kill () on the subprocess.

This flag was ignored if create_new_console is specified.

More happy, thought can solve the problem, the results of a half-day test, only to understand that this thing is only windows, I go ah, but think of, win can do, Linux certainly can, so locate to

Preexec_fn

Another Google, not an object, got a Setpgid (0,0) test, the child process or the keynote process belongs to the same process group, and later brainwave:

PREEXEC_FN = Os.setpgrp this solves the problem of the new build process group. continue to work, the back of the encounter is the problem of zombie process, os.waitpid a bit to solve. At the beginning of the Waitpid, still on the Linxu man on a half-day, looking at the parameters in the Linxu manual, or not rest assured Ah, the result Python os.waitpid unexpectedly not so many parameters, and no return value, simple AH. But it's solving my problem.  here is the full test code for today.    [email protected] kill-subprocess]$ cat sub-process.pyImport subprocessImport OSImport Time  def my_func ():#派生两个子进程, the child process derives several sleep grandchild processes, primarily 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 ()#测试了一些个The value of PREEXEC_FN, finally found to be able to use, the concept of Python objects still do not understand Ah, novice, novice.#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 PID pid = My_func ()#p. Wait ()print "Now, sleep 2s, then, Os.kill gpid%d"% pidTime.sleep () a = Os.kill (-pid, 9)print "Kill,return:"Print a # Kill, I tested the kill without permission of the root process, will error: Permissions do not allow# Test Kill P P2 all 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 (+) print "Done ..." #p. Terminate ()#p. Kill ()#p. Wait ()##time. Sleep (max)#os. Kill (PID, 9)

Python subprocess kill all derived child processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.