defkill_child_processes(parent_pid, sig=signal.SIGTERM): """kill all child processes recursively""" try: parent = psutil.Process(parent_pid) except psutil.NoSuchProcess: return children = parent.children(recursive=True) for process in children: try: process.send_signal(sig) except psutil.NoSuchProcess: return
def_execute_func(func, queue, args, kwargs): """execute function and return the result or exception to a queue""" try: res = func(*args, **kwargs) except Exception as exc: # pylint: disable=broad-except res = exc queue.put(res)
defcall_with_timeout(queue, timeout, func, args, kwargs): """A wrapper to support timeout of a function call"""
# start a new process for timeout (cannot use thread because we have c function) p = Process(target=_execute_func, args=(func, queue, args, kwargs)) p.start() p.join(timeout=timeout)