def reap_children(recursive=False):
 if recursive:
  children = psutil.Process().children(recursive=True)
 else:
  children = []
 subprocs = _subprocesses_started.copy()
 _subprocesses_started.clear()
 for subp in subprocs:
  try:
   subp.terminate()
  except OSError as err:
   if (err.errno != errno.ESRCH):
    raise
  if subp.stdout:
   subp.stdout.close()
  if subp.stderr:
   subp.stderr.close()
  try:
   if subp.stdin:
    subp.stdin.close()
  finally:
   try:
    subp.wait()
   except OSError as err:
    if (err.errno != errno.ECHILD):
     raise
 if children:
  for p in children:
   try:
    p.terminate()
   except psutil.NoSuchProcess:
    pass
  (gone, alive) = psutil.wait_procs(children, timeout=GLOBAL_TIMEOUT)
  for p in alive:
   warn(("couldn't terminate process %r; attempting kill()" % p))
   try:
    p.kill()
   except psutil.NoSuchProcess:
    pass
  (_, alive) = psutil.wait_procs(alive, timeout=GLOBAL_TIMEOUT)
  if alive:
   for p in alive:
    warn(('process %r survived kill()' % p))

def reap_children ( recursive = False ) : if recursive : children = psutil . Process ( ) . children ( recursive = True ) else : children = [ ] subprocs = _subprocesses_started . copy ( ) _subprocesses_started . clear ( ) for subp in subprocs : try : subp . terminate ( ) except OSError as err : if ( err . errno != errno . ESRCH ) : raise if subp . stdout : subp . stdout . close ( ) if subp . stderr : subp . stderr . close ( ) try : if subp . stdin : subp . stdin . close ( ) finally : try : subp . wait ( ) except OSError as err : if ( err . errno != errno . ECHILD ) : raise if children : for p in children : try : p . terminate ( ) except psutil . NoSuchProcess : pass ( gone , alive ) = psutil . wait_procs ( children , timeout = GLOBAL_TIMEOUT ) for p in alive : warn ( ( "couldn't terminate process %r; attempting kill()" % p ) ) try : p . kill ( ) except psutil . NoSuchProcess : pass ( _ , alive ) = psutil . wait_procs ( alive , timeout = GLOBAL_TIMEOUT ) if alive : for p in alive : warn ( ( 'process %r survived kill()' % p ) )
when a child process dies .