Skip to content

Instantly share code, notes, and snippets.

@thepaul
Created July 9, 2015 21:29
Show Gist options
  • Save thepaul/0e611da8b4fadec94568 to your computer and use it in GitHub Desktop.
Save thepaul/0e611da8b4fadec94568 to your computer and use it in GitHub Desktop.
wait for all subprocesses in a list to exit
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