Skip to content

Commit d8db675

Browse files
committed
ENH: Limit thread-safe logging to python>=3.2
Thread safe logging is implemented using QueueHandler and QueueListener, which are only available in python versions >= 3.2. Therefore, only implement this for appropriate python versions. For lower versions (most notably, version 2.7), this will only affect the log files. Here, conflicts may arise due to racing conflicts, where multiple workers try to write to the log file at the same time. This may result in poorly or incorrectly formatted log entries.
1 parent 93c5d6e commit d8db675

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

radiomics/scripts/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,17 +357,24 @@ def _configureLogging(args):
357357
}
358358
}
359359

360+
if args.jobs > 1:
361+
# Update the logger format to include the threadname if multiprocessing
362+
# is enabled
363+
logging_config['formatters']['default']['format'] = \
364+
'[%(asctime)s] %(levelname)-.1s: (%(threadName)s) %(name)s: %(message)s'
365+
360366
# Set up optional logging to file
361367
if args.log_file is not None:
362-
if args.jobs > 1:
368+
py_version = (sys.version_info.major, sys.version_info.minor)
369+
if args.jobs > 1 and py_version >= (3, 2):
363370
# Multiprocessing! Use a QueueHandler, FileHandler and QueueListener
364371
# to implement thread-safe logging.
372+
373+
# However, QueueHandler and Listener were added in python 3.2.
374+
# Therefore, only use this if the python version > 3.2
365375
q = Manager().Queue(-1)
366376
threading.current_thread().setName('Main')
367377

368-
logging_config['formatters']['default']['format'] = \
369-
'[%(asctime)s] %(levelname)-.1s: (%(threadName)s) %(name)s: %(message)s'
370-
371378
logging_config['handlers']['logfile'] = {
372379
'class': 'logging.handlers.QueueHandler',
373380
'queue': q,

0 commit comments

Comments
 (0)