Created
July 9, 2015 21:29
-
-
Save thepaul/0e611da8b4fadec94568 to your computer and use it in GitHub Desktop.
wait for all subprocesses in a list to exit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import signal | |
def wait_for_subprocs(procs, cb=lambda proc: 0): | |
# do-nothing handler for SIGCHLD, just so it's something other than SIG_DFL. | |
# otherwise, Python won't interrupt syscalls. | |
oldhandler = signal.signal(signal.SIGCHLD, lambda *_: None) | |
try: | |
while procs: | |
signal.pause() | |
aliveprocs = [] | |
for p in procs: | |
if p.poll() is None: | |
aliveprocs.append(p) | |
else: | |
cb(p) | |
procs = aliveprocs | |
finally: | |
signal.signal(signal.SIGCHLD, oldhandler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment