Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread safe logging #441

Merged
merged 4 commits into from Oct 22, 2018
Merged

Conversation

JoostJM
Copy link
Collaborator

@JoostJM JoostJM commented Oct 18, 2018

Implement thread-safe logging to a file by using a QueueHandler and QueueListener to process the log records intended for the logfile. This ensures only 1 thread is writing to the file, preventing corruption in case of collisions.

Additionally, improve the error catching code in the commandline script to allow for cancellation by the user (via KeyboardInterrupt/ SystemExit and in case of parallel processing; by calling pool.terminate())

cc @Radiomics/developers

When extracting features in parallel, conflicts may arise when writing to the log-file, corrupting the records.
To prevent this from happening, use a QueueHandler in combination with a QueueListener to process log records for the log file.

Log messages written to the console are not affected.

Additionally, use dictConfig to configure the logging.

Finally, do not catch the Keyboard and SystemExit errors.
Capture KeyboardInterrupts and SystemExit errors separately to allow for cancellation of batch jobs.
In case of parallel processing, slightly more needs to be done:
- Use map_async to start the parallel processing in a non-blocking mode
- Use a while loop to check if the process is done and only then get the results. This allows the main process to react to a keyboard interrupt.
- When the main process is cancelled, terminate the pool to stop further processing of jobs.
- When extracting in serial, propagate the cancellation errors, but just return None in case of parallel extraction.
@JoostJM JoostJM merged commit ab1bcaa into AIM-Harvard:master Oct 22, 2018
@JoostJM JoostJM deleted the thread-safe_logging branch October 22, 2018 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant