Skip to main content

multiprocessing-logging

Supported Python versions License

When using the multiprocessing module, logging becomes less useful since sub-processes should log to individual files/streams or there's the risk of records becoming garbled.

This simple module implements a Handler that when set on the root Logger will handle tunneling the records to the main process so that they are handled correctly.

It's currently tested in Linux on CPython 2.7, CPython 3.9+, and PyPy 2.7 and 3.

Only works on POSIX systems and only Linux is supported. It does not work on Windows.

Origin

This library was taken verbatim from a StackOverflow post and extracted into a module so that I wouldn't have to copy the code in every project.

Later, several improvements have been contributed.

Usage

Before you start logging but after you configure the logging framework (maybe with logging.basicConfig(...)), do the following:

import multiprocessing_logging

multiprocessing_logging.install_mp_handler()

and that's it.

With multiprocessing.Pool

When using a Pool, make sure install_mp_handler is called before the Pool is instantiated, for example:

import logging
from multiprocessing import Pool
from multiprocessing_logging import install_mp_handler

logging.basicConfig(...)
install_mp_handler()
pool = Pool(...)

Problems

The approach of this module relies on fork being used to create new processes. This start method is basically unsafe when also using threads, as this module does.

The consequence is that there's a low probability of the application hanging when creating new processes.

As a palliative, don't continuously create new processes. Instead, create a Pool once and reuse it.

Since Python 3.14 the default start method on Linux is forkserver, so on those versions you must call multiprocessing.set_start_method("fork") before install_mp_handler(). Otherwise it raises an AssertionError.

Metadata

Release files for multiprocessing-logging 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for multiprocessing-logging 0.4.1
File Size Uploaded Provenance Actions
multiprocessing_logging-0.4.1.tar.gz 11.0 kB Without provenance Details for multiprocessing_logging-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for multiprocessing-logging 0.4.1
File Interpreter ABI Platform Actions
multiprocessing_logging-0.4.1-py3-none-any.whl Python 3 none any Details for multiprocessing_logging-0.4.1-py3-none-any.whl

Total release size: 18.4 kB

Release files / multiprocessing_logging-0.4.1.tar.gz

Download URL multiprocessing_logging-0.4.1.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
897f0151e55f662525d83838eab2c297baefad2832bf4c81f19c327a60886520
BLAKE2b-256 checksum
How to use checksums
895afe7db9bed7517d016735c0f69ac168a1d58c164bc5da16cb7b6b90415962
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Release files / multiprocessing_logging-0.4.1-py3-none-any.whl

Download URL multiprocessing_logging-0.4.1-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aad377d4c94592a9977e9da95cce35417e3cb251c7aa5bab94f0bf5c0061f4d5
BLAKE2b-256 checksum
How to use checksums
6117789df4fa01b31b306597daedc7381d9eb170e3d9c4a679c38278c032f572
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.7

Security for multiprocessing-logging 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page