Logging and debugging are great ways to get insights into your programs, especially while developing code.
Multiprocessing in Python introduces some quirks that make it more challenging to develop and monitor programs.
Using VSCode on Windows, I have not been able to use the debugger without crashing, so I switched to using logging for the same purpose.
Print statements can help, but logging gives a much nicer interface and ability to send outputs to different locations.
Naive Logging Approach
After the debugger VSCode debugger crashed for me, I switched to logging, writing some code like this.
(See my previous post on basic multiprocessing)
It seems sensible to just pass the logger to the my_function and have it operate normally.
This seems to work!
Unfortunately, logging to a single file from multiple processes is not supported, and you will end up with some frustrating bugs.
Queue Handler Approach to take Logging Events
Using the logging cookbook as a starting point, I’ve developed a few relatively simple functions to help facilitate logging with multiprocesses code.
The solution I’ve settled on is to create a listener process that monitors a queue.
This queue is passed to the child processes, and they are able to log to the queue and the listener process sends the messages to the log file.
Output:
2020-06-06 13:56:56,531 - my_log - INFO - hello from before process
2020-06-06 13:56:56,825 - my_log.child_0 - INFO - hello from inside process
2020-06-06 13:56:56,834 - my_log.child_1 - INFO - hello from inside process
2020-06-06 13:56:56,847 - my_log.child_2 - INFO - hello from inside process
2020-06-06 13:56:56,849 - my_log - INFO - result of process: 1
2020-06-06 13:56:56,850 - my_log - INFO - result of process: 2
2020-06-06 13:56:56,850 - my_log - INFO - result of process: 3
Summary
Creating a few simple function to help with multiprocessing logging can make life a lot easier.
I keep these function in a library so they can be called when needed.
This process works on Windows/Linux