subprocess - python: simple approach to killing children or reporting their success? -


I

  1. In parallel to call shell commands (for example, 'sleep' below)
  2. Report on your personal start and finish and be able to kill them with 'kill-9 parent_process_pid'.

There are already so many things written on this type of things but I think that I found a very beautiful Python solution which I am looking for.

put subprocess.call in a wrapper function (unix_command ) Which reports the beginning and completion of the order.

  • Call the wrapper function with multi-process. process.
  • Track the appropriate piids, store them globally, and kill them in Signal_Handler.
  • I was trying to avoid a solution that called processes from time to time but I'm not sure why.

    Is there a better way?

      Import subprocess, multi processing, signal import system, OS, time DEF signature handler (signal, frame): print 'you killed me!' For P in Pids: os.kill (p, 9) sys.exit (0) def sigint_handler (signal, frame): print 'you press Ctrl + C! Sys.exit (0) sign.signal (signal SIGINT, sigint_handler) signal.signal (signal sigterm, sigterm_handler) def f_wrapper (d): print str (d) + "start" p = subprocess.call (["sleep", "100"]) pids.append (p.pid) print str (d) + "done" print "starts to run things." Pids = [] in the range for (5): p = multiprocessing.Process ( Target = f_wrapper, args = (i,)) p.daemon = True p.start () print "things are going ..." pids while: print "still working ..." time.sleep ( 1)    

    Return once subtype - and call Return value subtype of Rkriya's return code . Therefore, depositing those return codes in the list pids (which is not synchronized between multiprocessing by BTW and between the "main" process) and them 9 Sending signals "such as" they were process IDs instead of return codes, it's definitely wrong.

    There is definitely a wrong question:

    Be able to kill them with 'kill-9 parent_process_pid' Since -9 means that the basic process can not block the signal ( purpose -9 Code> to explicitly specify) - I think that -9 is therefore fake.

    Instead of threading multi-processing (every "midwife" thread, or process, is not necessarily anything but its sub-process So why waste procedures on such a light work?); You should call suprocess.process in the main thread (the sub-process has been put in the list to begin and to be able to get its .pid ) As a result, the result of the resultant process of the dual thread is found which waits for it (and when it is reported and removes it from the list) the list of sub-process IDs should be protected by lock, If the thread and many right formula can all access it, and would be a better option than a set possibly a list (fast removal) because you do not care to order and not only about avoiding duplicate.

    So, almost (there is no test, so can be bugs ;-) I will change my code as s / thing:

      import subprocessing, threading, Signal import system, time pobbs = set () pobslock = threading.Lock () def numpobs (): with podsoc: return lane (pobes) def sigterm_handler (signal, frame): print 'you killed me!' With Pobslock: For p in pobs: p.kill () sys.exit (0) def sigint_handler (signal, frame): print 'you pressed Ctrl + C! Sys.exit (0) sign.signal (signal SIGINT, sigint_handler) signal.signal (signal sigterm, sigterm_handler) def f_wrapper (d, p): print d, 'start', ppid rc = p. White (with Pobslock): pobs.remove (P) print D, 'done, RC =', RC print "things start to run." Pobs.add (p) t = threading.Thread (with target = f_wrapper, args = (i, p): (= '' sleep '', '100']) range (i) for category (5) : P = subprocess.Popen) T.daemon = True t.start () Print "Moves things ..." while numpobs (): print "still working ..." time.sleep ( 1)  

    Comments

    Popular posts from this blog

    Eclipse CDT variable colors in editor -

    AJAX doesn't send POST query -

    wpf - Custom Message Box Advice -