Skip to content

Possibility of multiprocess-safe logging without inheriting the logger? #924

@julian-goettingen

Description

@julian-goettingen

I have had problems with colorized output on multiprocessing with windows and I did find the solution here:
#108

Unfortunately in my case the creation of the multiprocessing-pool is within a library.
Now I can solve the problem by patching the library (I probably will) but I wonder if a solution without touching initargs is possible, for the following reason:

In 2019 when the issue linked above was raised the newest stable python-version was 3.7 and this problem only affected windows.
But with python3.8, macOS switched the default to spawn and in python3.14, even linux will switch the default to spawn, thus eventually making default multiprocessing consistent across platforms. https://docs.python.org/3.12/library/multiprocessing.html

Here is an idea, no idea if it is feasible with the library-internals of loguru:
multiprocessing-processes know their parent-process and can obtain its pid (even if they were spawned instead of forked) This unnecessarily wordy article shows it: https://superfastpython.com/multiprocessing-parent/
(This is a python>=3.8 feature, but since 3.7 is EOL I think that's ok)
So if the main process expects more processes to create their own global loguru-loggers, then it would be theoretically possible for it to set up a synchronization-scheme in advance that the other processes will discover and hook into when those processes initialize their global loguru-logger.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions