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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

multiprocessing_logging-0.4.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

multiprocessing_logging-0.4.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file multiprocessing_logging-0.4.0.tar.gz.

File metadata

  • Download URL: multiprocessing_logging-0.4.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.7

File hashes

Hashes for multiprocessing_logging-0.4.0.tar.gz
Algorithm Hash digest
SHA256 152d209f4d1fd7fed500e695c92848bd72016f3a54b98a80283d64abcc27e0bc
MD5 38c80583d9da4b842840ffc34412a97f
BLAKE2b-256 e49d5751e234a12525994a8b41a962b4ab974f4ac074eb7d5367ba94440b2d57

See more details on using hashes here.

File details

Details for the file multiprocessing_logging-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for multiprocessing_logging-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d76dae0f187343d9009aec4cada11e7323ba8189869e11797ddeab3c90dc687
MD5 2adcc3dd779817c12f69ed57df652cdd
BLAKE2b-256 99a11affd609ca8e22383f56ba1e4289e47bc2f11af0a0ae4f2b944aac4a6616

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 files

0.3.4

1 file

0.3.3

1 file

0.3.2

1 file

0.3.1

1 file

0.3.0

1 file

0.2.6

1 file

0.2.5

2 files

0.2.4

1 file

0.2.3

1 file

0.2.2

1 file

0.2.1

2 files

0.2

2 files

0.1

2 files

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