question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Messages are getting overlapped when logging in the sys.stderr using multiprocessing

See original GitHub issue

I am trying to log to sys.stderr using multiprocessing but the log messages are getting overlapped. Is this the intended behavior?

2019-07-01 15:19:49.197 | SUCCESS  | __mp_main__:worker:14 - My function executed successfully
2019-07-01 15:19:49.2052019-07-01 15:19:49.205 |  | SUCCESS SUCCESS  |  | __mp_main____mp_main__::workerworker::1414 -  - My function executed successfullyMy function executed successfully

2019-07-01 15:19:50.198 | SUCCESS  | __mp_main__2019-07-01 15:19:50.200: | workerSUCCESS : | 14__mp_main__ - :My function executed successfullyworker
:14 - My function executed successfully
2019-07-01 15:19:50.205 | SUCCESS  | __mp_main__:worker:14 - My function executed successfully
2019-07-01 15:19:50.209 | SUCCESS  | __mp_main__:worker:14 - My function executed successfully
2019-07-01 15:19:51.207 | SUCCESS  | __mp_main__:worker:14 - My function executed successfully
2019-07-01 15:19:52.198 | SUCCESS  | __mp_main__:worker:14 - My function executed successfully
2019-07-01 15:19:52.209 | SUCCESS  | __mp_main__:worker:14 - My function executed successfully

Here is the code that generated the above output.

import sys
import time
import random
from concurrent.futures import ProcessPoolExecutor

from loguru import logger

logger.remove()
logger.add(sys.stderr, enqueue=True)


def worker(seconds):
    time.sleep(seconds)
    logger.success("My function executed successfully")


def main():

    with ProcessPoolExecutor() as executor:
        seconds = [random.randint(1, 3) for i in range(10)]
        executor.map(worker, seconds)


if __name__ == "__main__":
    main()

Cheers, Chris

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:25 (14 by maintainers)

github_iconTop GitHub Comments

3reactions
Delgancommented, Dec 1, 2019

I can finally close this issue! v0.4.0 has just been released and should solve this problem. 👍

As a reminder:

I added some code snippets and explanation in the documentation, I highly recommend to read it if you need to deal with multiprocessing on Windows:

2reactions
Delgancommented, Oct 20, 2019

Ok, I finally managed to fix that. 😛

Dealing with multiprocessing on Windows is really painful, especially considering that Loguru heavily relies on one unique global logger.

I had to rethink the management of handlers (see #157), it’s not perfect but the final solution is satisfactory enough in my opinion.

Basically, you will need to make your child processes “inherit” from the parent logger. It should not be passed as an argument once the child is started, it should be passed during construction of the child. To do so, most multiprocessing functions offer initializer and initargs arguments. This also requires added handlers to be picklable or, alternatively, to be added with enqueue=True.

So, the initial snippet of this issue may look like this once updated:

import sys
import time
import random
from concurrent.futures import ProcessPoolExecutor

from loguru import logger

def set_logger(logger_):
    global logger
    logger = logger_

def worker(seconds):
    time.sleep(seconds)
    logger.success("My function executed successfully")


def main():

    with ProcessPoolExecutor(initializer=set_logger, initargs=(logger, )) as executor:
        seconds = [random.randint(1, 3) for i in range(10)]
        executor.map(worker, seconds)


if __name__ == "__main__":
    logger.remove()
    logger.add(sys.stderr, enqueue=True)

    main()

I added some code snippets and explanation in the documentation, I highly recommend to read it if you need to deal with multiprocessing on Windows:

I will leave this issue open until next loguru release. In the meantine, feel free to suggest improvements to the documentation or ask any question.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overlapping print lines in multiprocessing - Stack Overflow
I found myself dealing with multiprocessing recently and I have about 10 processes that are printing simultaneously on different files but ...
Read more >
Multiprocessing Logging in Python
This logger is used within objects and functions within the multiprocessing module to log messages, such as debug messages that processes are ...
Read more >
tqdm - PyPI
Manual control of tqdm() updates using a with statement: with tqdm(total=100) as ... Specifies where to output the progress messages (default: sys.stderr).
Read more >
Output synchronisation in Python - EJRH - WordPress.com
One of the first problems that a programmer might encounter when writing a multithreaded program is the interleaving of outputs from multiple ...
Read more >
Profiler User's Guide - NVIDIA Documentation Center
It is recommended to use next-generation tools NVIDIA Nsight Systems for GPU and CPU ... you can use the guided analysis system to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found