Add config sdk.development.worker.console_cr_flush_period

This commit is contained in:
allegroai 2020-11-22 01:15:35 +02:00
parent 6e012cb205
commit a3ad6a0e50
2 changed files with 12 additions and 12 deletions

View File

@ -171,7 +171,7 @@ class PrintPatchLogger(object):
patched = False
lock = threading.Lock()
recursion_protect_lock = threading.RLock()
lf_flush_period = config.get("development.worker.console_lf_flush_period", 0)
cr_flush_period = config.get("development.worker.console_cr_flush_period", 0)
def __init__(self, stream, logger=None, level=logging.INFO):
PrintPatchLogger.patched = True
@ -196,16 +196,16 @@ class PrintPatchLogger(object):
do_cr = '\r' in message
self._cur_line += message
if not do_flush and do_cr and PrintPatchLogger.lf_flush_period and self._force_lf_flush:
if not do_flush and do_cr and PrintPatchLogger.cr_flush_period and self._force_lf_flush:
self._cur_line += '\n'
do_flush = True
self._force_lf_flush = False
if (not do_flush and (PrintPatchLogger.lf_flush_period or not do_cr)) or not message:
if (not do_flush and (PrintPatchLogger.cr_flush_period or not do_cr)) or not message:
return
if PrintPatchLogger.lf_flush_period and self._cur_line:
if PrintPatchLogger.cr_flush_period and self._cur_line:
self._cur_line = '\n'.join(line.split('\r')[-1] for line in self._cur_line.split('\n'))
last_lf = self._cur_line.rindex('\n' if do_flush else '\r')
@ -263,11 +263,11 @@ class LogFlusher(threading.Thread):
self._period = period
self._logger = logger
self._exit_event = threading.Event()
self._lf_last_flush = 0
self._cr_last_flush = 0
try:
self._lf_flush_period = float(PrintPatchLogger.lf_flush_period)
self._cr_flush_period = float(PrintPatchLogger.cr_flush_period)
except (ValueError, TypeError):
self._lf_flush_period = 0
self._cr_flush_period = 0
@property
def period(self):
@ -279,12 +279,12 @@ class LogFlusher(threading.Thread):
while True:
period = self._period
while not self._exit_event.wait(period or 1.0):
if self._lf_flush_period and time() - self._lf_last_flush > self._lf_flush_period:
if self._cr_flush_period and time() - self._cr_last_flush > self._cr_flush_period:
if isinstance(sys.stdout, PrintPatchLogger):
sys.stdout.force_lf_flush()
if isinstance(sys.stderr, PrintPatchLogger):
sys.stderr.force_lf_flush()
self._lf_last_flush = time()
self._cr_last_flush = time()
# now signal the real flush
self._logger.flush()

View File

@ -176,9 +176,9 @@
# Log all stdout & stderr
log_stdout: true
# Line feed (\r) support. If zero (0) \r treated as \n and flushed to backend
# Line feed flush support in seconds, flush consecutive line feeds (\r) every X (default: 10) seconds
console_lf_flush_period: 10
# Carriage return (\r) support. If zero (0) \r treated as \n and flushed to backend
# Carriage return flush support in seconds, flush consecutive line feeds (\r) every X (default: 10) seconds
console_cr_flush_period: 10
# compatibility feature, report memory usage for the entire machine
# default (false), report only on the running process and its sub-processes