Messages are getting overlapped when logging in the sys.stderr using multiprocessing
See original GitHub issueI 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:
- Created 4 years ago
- Reactions:1
- Comments:25 (14 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I can finally close this issue!
v0.4.0
has just been released and should solve this problem. 👍As a reminder:
Ok, I finally managed to fix that. 😛
Dealing with
multiprocessing
on Windows is really painful, especially considering that Loguru heavily relies on one unique globallogger
.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 offerinitializer
andinitargs
arguments. This also requires added handlers to be picklable or, alternatively, to be added withenqueue=True
.So, the initial snippet of this issue may look like this once updated:
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:multiprocessing
usingenqueue
argumentI will leave this issue open until next
loguru
release. In the meantine, feel free to suggest improvements to the documentation or ask any question.