'Internal routine to wait for children that have exited.'

def collect_children(self):

    if (self.active_children is None):
        return
    while (len(self.active_children) >= self.max_children):
        try:
            (pid, _) = os.waitpid((-1), 0)
            self.active_children.discard(pid)
        except OSError as e:
            if (e.errno == errno.ECHILD):
                self.active_children.clear()
            elif (e.errno != errno.EINTR):
                break
    for pid in self.active_children.copy():
        try:
            (pid, _) = os.waitpid(pid, os.WNOHANG)
            self.active_children.discard(pid)
        except OSError as e:
            if (e.errno == errno.ECHILD):
                self.active_children.discard(pid)
