Last active
December 7, 2019 05:08
-
-
Save schipiga/1f14299df22cf88adc1cd88b4a8ce884 to your computer and use it in GitHub Desktop.
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 psutil | |
def _proc_children(): # added by me | |
for child in psutil.Process(os.getpid()).children(recursive=True): | |
yield child | |
def _exit(exit_code): # added by me | |
for child in _proc_children(): | |
child.kill() | |
os._exit(exit_code) | |
def worker_process(params, channel): # existing function | |
"""The worker process routines.""" | |
signal.signal(signal.SIGINT, signal.SIG_IGN) | |
signal.signal(signal.SIGTERM, lambda signum, frame: _exit(0)) # added by me | |
if params.initializer is not None: | |
if not run_initializer(params.initializer, params.initargs): | |
_exit(1) | |
# store children pids which may start together with initializer | |
init_pids = [child.pid for child in _proc_children()] # added by me | |
try: | |
for task in worker_get_next_task(channel, params.max_tasks): | |
payload = task.payload | |
result = process_execute( | |
payload.function, *payload.args, **payload.kwargs) | |
send_result(channel, Result(task.id, result)) | |
# added by me | |
for child in _proc_children(): | |
# kill child process if it wasn't finished together with task | |
if child.pid not in init_pids: | |
child.kill() | |
except (EnvironmentError, OSError, RuntimeError) as error: | |
_exit(error.errno if error.errno else 1) | |
except EOFError: | |
_exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment