mirror of
https://github.com/clearml/clearml
synced 2025-06-23 01:55:38 +00:00
Fix ClearML SDK does not work with PyTest (related to https://github.com/pytest-dev/pytest/issues/5502#issuecomment-647157873)
This commit is contained in:
parent
65d0e15a74
commit
7016138c84
@ -19,10 +19,10 @@ class StdStreamPatch(object):
|
|||||||
(not running_remotely() or DEBUG_SIMULATE_REMOTE_TASK.get()):
|
(not running_remotely() or DEBUG_SIMULATE_REMOTE_TASK.get()):
|
||||||
StdStreamPatch._stdout_proxy = PrintPatchLogger(
|
StdStreamPatch._stdout_proxy = PrintPatchLogger(
|
||||||
sys.stdout, a_logger, level=logging.INFO, load_config_defaults=load_config_defaults) \
|
sys.stdout, a_logger, level=logging.INFO, load_config_defaults=load_config_defaults) \
|
||||||
if connect_stdout else None
|
if connect_stdout and not sys.stdout.closed else None
|
||||||
StdStreamPatch._stderr_proxy = PrintPatchLogger(
|
StdStreamPatch._stderr_proxy = PrintPatchLogger(
|
||||||
sys.stderr, a_logger, level=logging.ERROR, load_config_defaults=load_config_defaults) \
|
sys.stderr, a_logger, level=logging.ERROR, load_config_defaults=load_config_defaults) \
|
||||||
if connect_stderr else None
|
if connect_stderr and not sys.stderr.closed else None
|
||||||
|
|
||||||
if StdStreamPatch._stdout_proxy:
|
if StdStreamPatch._stdout_proxy:
|
||||||
# noinspection PyBroadException
|
# noinspection PyBroadException
|
||||||
@ -199,11 +199,15 @@ class PrintPatchLogger(object):
|
|||||||
self._test_lr_flush()
|
self._test_lr_flush()
|
||||||
|
|
||||||
self.lock.acquire()
|
self.lock.acquire()
|
||||||
|
# noinspection PyBroadException
|
||||||
|
try:
|
||||||
with PrintPatchLogger.recursion_protect_lock:
|
with PrintPatchLogger.recursion_protect_lock:
|
||||||
if hasattr(self._terminal, '_original_write'):
|
if hasattr(self._terminal, '_original_write'):
|
||||||
self._terminal._original_write(message) # noqa
|
self._terminal._original_write(message) # noqa
|
||||||
else:
|
else:
|
||||||
self._terminal.write(message)
|
self._terminal.write(message)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
do_flush = '\n' in message
|
do_flush = '\n' in message
|
||||||
# check for CR character
|
# check for CR character
|
||||||
@ -245,10 +249,14 @@ class PrintPatchLogger(object):
|
|||||||
# what can we do, nothing
|
# what can we do, nothing
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
# noinspection PyBroadException
|
||||||
|
try:
|
||||||
if hasattr(self._terminal, '_original_write'):
|
if hasattr(self._terminal, '_original_write'):
|
||||||
self._terminal._original_write(message) # noqa
|
self._terminal._original_write(message) # noqa
|
||||||
else:
|
else:
|
||||||
self._terminal.write(message)
|
self._terminal.write(message)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
def connect(self, logger):
|
def connect(self, logger):
|
||||||
# refresh if needed
|
# refresh if needed
|
||||||
|
@ -3534,6 +3534,16 @@ class Task(_Task):
|
|||||||
# # we have to forcefully shutdown if we have forked processes, sometimes they will get stuck
|
# # we have to forcefully shutdown if we have forked processes, sometimes they will get stuck
|
||||||
# os._exit(self.__exit_hook.exit_code if self.__exit_hook and self.__exit_hook.exit_code else 0)
|
# os._exit(self.__exit_hook.exit_code if self.__exit_hook and self.__exit_hook.exit_code else 0)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _clear_loggers():
|
||||||
|
# https://github.com/pytest-dev/pytest/issues/5502#issuecomment-647157873
|
||||||
|
import logging
|
||||||
|
loggers = [logging.getLogger()] + list(logging.Logger.manager.loggerDict.values())
|
||||||
|
for logger in loggers:
|
||||||
|
handlers = getattr(logger, 'handlers', [])
|
||||||
|
for handler in handlers:
|
||||||
|
logger.removeHandler(handler)
|
||||||
|
|
||||||
def __shutdown(self):
|
def __shutdown(self):
|
||||||
"""
|
"""
|
||||||
Will happen automatically once we exit code, i.e. atexit
|
Will happen automatically once we exit code, i.e. atexit
|
||||||
@ -3560,6 +3570,8 @@ class Task(_Task):
|
|||||||
# from here only a single thread can re-enter
|
# from here only a single thread can re-enter
|
||||||
self._at_exit_called = get_current_thread_id()
|
self._at_exit_called = get_current_thread_id()
|
||||||
|
|
||||||
|
Task._clear_loggers()
|
||||||
|
|
||||||
# disable lock on signal callbacks, to avoid deadlocks.
|
# disable lock on signal callbacks, to avoid deadlocks.
|
||||||
if self.__exit_hook and self.__exit_hook.signal is not None:
|
if self.__exit_hook and self.__exit_hook.signal is not None:
|
||||||
self.__edit_lock = False
|
self.__edit_lock = False
|
||||||
|
Loading…
Reference in New Issue
Block a user