View difference between Paste ID: 6cu9APE2 and
SHOW:
|
|
- or go back to the newest paste.
| 1 | - | |
| 1 | + | from signal import SIGTERM |
| 2 | from sys import exit | |
| 3 | from time import sleep | |
| 4 | from os import * | |
| 5 | ||
| 6 | def slow(): | |
| 7 | fork() | |
| 8 | sleep(10) | |
| 9 | ||
| 10 | def fast(): | |
| 11 | sleep(1) | |
| 12 | ||
| 13 | child_pids = [] | |
| 14 | #for child_func in [fast, slow, slow, fast]: | |
| 15 | for child_func in [fast, fast]: | |
| 16 | pid = fork() | |
| 17 | if pid == 0: | |
| 18 | setsid() | |
| 19 | child_func() | |
| 20 | exit(0) | |
| 21 | else: | |
| 22 | print "spawned pgrp %d" % pid | |
| 23 | child_pids.append(pid) | |
| 24 | ||
| 25 | (pid, status) = waitpid(-1, 0) | |
| 26 | print "Reaped pid: %d, status: %d" % (pid, status) | |
| 27 | for child_pid in child_pids: | |
| 28 | try: | |
| 29 | print popen('ps -e -o pid,ppid,pgid,state,command | grep -i python').read()
| |
| 30 | print "killing pg %d" % child_pid | |
| 31 | killpg(child_pid, SIGTERM) | |
| 32 | except OSError, e: | |
| 33 | print "Error killing %s: %s" %(child_pid, e) |