Fix creating subprocess from a daemon process

This commit is contained in:
allegroai 2021-02-01 23:40:48 +02:00
parent 1c1fecf7d0
commit 3890477056

View File

@ -206,7 +206,24 @@ class BackgroundMonitor(object):
d.set_subprocess_mode() d.set_subprocess_mode()
BackgroundMonitor._main_process = Process(target=cls._background_process_start, args=(id(task), )) BackgroundMonitor._main_process = Process(target=cls._background_process_start, args=(id(task), ))
BackgroundMonitor._main_process.daemon = True BackgroundMonitor._main_process.daemon = True
# Hack allow to create daemon subprocesses (even though python doesn't like it)
un_daemonize = False
# noinspection PyBroadException
try:
from multiprocessing import current_process
if current_process()._config.get('daemon'): # noqa
un_daemonize = current_process()._config.get('daemon') # noqa
current_process()._config['daemon'] = False # noqa
except BaseException:
pass
BackgroundMonitor._main_process.start() BackgroundMonitor._main_process.start()
if un_daemonize:
# noinspection PyBroadException
try:
from multiprocessing import current_process
current_process()._config['daemon'] = un_daemonize # noqa
except BaseException:
pass
# wait until subprocess is up # wait until subprocess is up
if wait_for_subprocess: if wait_for_subprocess:
cls._sub_process_started.wait() cls._sub_process_started.wait()