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)
@staticmethod
def remove_std_logger():
def remove_std_logger(logger=None):
if isinstance(sys.stdout, PrintPatchLogger):
# noinspection PyBroadException
try:
sys.stdout.connect(None)
sys.stdout.disconnect(logger)
except Exception:
pass
if isinstance(sys.stderr, PrintPatchLogger):
# noinspection PyBroadException
try:
sys.stderr.connect(None)
sys.stderr.disconnect(logger)
except Exception:
pass
@ -172,6 +172,11 @@ class PrintPatchLogger(object):
self._cur_line = ''
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):
if attr in ['_log', '_terminal', '_log_level', '_cur_line']:
return self.__dict__.get(attr)

View File

@ -735,6 +735,9 @@ class Logger(object):
self._task_handler.flush()
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:
t = self._task_handler
self._task_handler = None