Disconnect stdout/stderr logger on exit

This commit is contained in:
allegroai 2020-04-09 12:31:43 +03:00
parent de9c88bc2d
commit 5ec4d80493
2 changed files with 11 additions and 3 deletions

View File

@ -77,17 +77,17 @@ class StdStreamPatch(object):
StdStreamPatch._stderr_proxy.connect(logger) StdStreamPatch._stderr_proxy.connect(logger)
@staticmethod @staticmethod
def remove_std_logger(): def remove_std_logger(logger=None):
if isinstance(sys.stdout, PrintPatchLogger): if isinstance(sys.stdout, PrintPatchLogger):
# noinspection PyBroadException # noinspection PyBroadException
try: try:
sys.stdout.connect(None) sys.stdout.disconnect(logger)
except Exception: except Exception:
pass pass
if isinstance(sys.stderr, PrintPatchLogger): if isinstance(sys.stderr, PrintPatchLogger):
# noinspection PyBroadException # noinspection PyBroadException
try: try:
sys.stderr.connect(None) sys.stderr.disconnect(logger)
except Exception: except Exception:
pass pass
@ -172,6 +172,11 @@ class PrintPatchLogger(object):
self._cur_line = '' self._cur_line = ''
self._log = logger self._log = logger
def disconnect(self, logger=None):
# disconnect the logger only if it was registered
if not logger or self._log == logger:
self.connect(None)
def __getattr__(self, attr): def __getattr__(self, attr):
if attr in ['_log', '_terminal', '_log_level', '_cur_line']: if attr in ['_log', '_terminal', '_log_level', '_cur_line']:
return self.__dict__.get(attr) return self.__dict__.get(attr)

View File

@ -735,6 +735,9 @@ class Logger(object):
self._task_handler.flush() self._task_handler.flush()
def _close_stdout_handler(self, wait=True): def _close_stdout_handler(self, wait=True):
# detach the sys stdout/stderr
StdStreamPatch.remove_std_logger(self)
if self._task_handler and DevWorker.report_stdout: if self._task_handler and DevWorker.report_stdout:
t = self._task_handler t = self._task_handler
self._task_handler = None self._task_handler = None