Multiprocessing-logging is a Python library that provides a handler to centralize logging from child processes created by the `multiprocessing` module to the main process. This prevents log messages from becoming garbled when multiple processes attempt to write to the same stream or file concurrently. It currently supports Python 3.9+ and is primarily tested on Linux.
pip install multiprocessing-loggingVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `multiprocessing-logging` with Python's standard `logging` module. Configure your root logger with `logging.basicConfig` first, then call `install_mp_handler()` before any `multiprocessing.Process` or `multiprocessing.Pool` instances are created. Note the critical warning about the 'fork' start method requirement.
On compatible POSIX systems, explicitly set the start method to 'fork' using `multiprocessing.set_start_method('fork', force=True)` at the very beginning of your main script, before any processes are created. Ensure your environment supports 'fork' safely.As a palliative, avoid continuously creating new processes. Instead, create a `multiprocessing.Pool` once and reuse it for your tasks.
Ensure `multiprocessing_logging.install_mp_handler()` is placed early in your `if __name__ == '__main__':` block, after your main process logging is configured but before any child processes are spawned.
Ensure `multiprocessing_logging.install_mp_handler()` is correctly called in the main process before any child processes are spawned, and that the root logger (or the logger passed to the handler) has appropriate handlers configured.
If your environment supports 'fork' and you understand its implications, explicitly set the start method at the very beginning of your script: `import multiprocessing; multiprocessing.set_start_method('fork', force=True)`.Avoid continuously creating new processes. Instead, use a `multiprocessing.Pool` and initialize it once, then reuse it for your tasks. This limits the number of `fork` operations.
No dependency data recorded yet.