This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Logging Cookbook Improvement
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.5
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, r.david.murray, vinay.sajip, yasuiniko
Priority: normal Keywords: patch

Created on 2016-08-23 12:22 by yasuiniko, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
mp_log_cookbook.patch yasuiniko, 2016-08-23 16:02 Simpler multiprocessing logging example. review
Messages (7)
msg273437 - (view) Author: Niko Yasui (yasuiniko) * Date: 2016-08-23 12:22
Removed the unnecessary listener function and changed the worker loggers from root to children. Also replaced print statements with logging statements and a stdout handler to illustrate the usefulness of handler levels. This version also doesn't require a manual listener process or thread, since it's all handled by the interpreter in the background. 

As far as I know, it's just a one-line change from multiprocessing to threading, so it should be easy to adapt this example to improve the threading example as well.

The previous example was hard for me to understand, and when I integrated it to my code, printed duplicates of every log item to all my handlers (including stdout) even though I hadn't added a stdout handler. Evidently this was my fault, since the original example code doesn't do that, but this updated code should be cleaner and easier to understand to avoid such integration problems in the first place. I hope it is a worthy contribution the docs. Feel free to provide any tips to improve it.
msg273447 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-08-23 13:52
Can you provide this change as a diff, please? (See docs.python.org/devguide for information on generating reviewable patches).
msg273455 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2016-08-23 14:31
I specifically wanted to illustrate a separate listener process - the purpose here is not to show what might be the most efficient code, but to show communication via a queue between listener process and worker processes. You've removed this, which defeats the object of this example (for example, there's no QueueListener in your version). So I wouldn't want to implement your proposed change in its current form.

Just to clarify, a diff of the cookbook ought to be a diff of the .rst file rather than just a changed code example within it - a direct comparison needs to be possible in order to see the changes easily in a side-by-side view.
msg273474 - (view) Author: Niko Yasui (yasuiniko) * Date: 2016-08-23 16:02
Thank you both for the helpful comments.

Vinay, since you want to illustrate a listener process in the multiprocessing example, why not include a shorter, simpler example beforehand? This is my first time using logging, and it took me multiple hours to figure out what your example was doing and which parts were extra. Since a queue isn't necessary for logging with multiprocessing, I did my best to write a shorter example to introduce the concept before exploring your code with the listener.

What do you think about this addition?
msg273482 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2016-08-23 16:31
> Since a queue isn't necessary for logging with multiprocessing

IMO a queue with QueueHandler/QueueListener is best practice when working with multiprocessing and logging. If you don't use them, things may seem to work some of the time but could fail in certain scenarios. For example, multiprocessing works differently on different platforms such as POSIX and Windows, which can lead to problems with simpler approaches.

There are plenty of simpler examples in the logging tutorial and cookbook, which might be worth reviewing first if you are new to logging in Python.

See this post for a discussion on using logging with multiprocessing:

https://plumberjack.blogspot.co.uk/2010/09/using-logging-with-multiprocessing.html

This was the original source of the example in the cookbook.
msg273527 - (view) Author: Niko Yasui (yasuiniko) * Date: 2016-08-24 00:56
I see, I didn't realize that the simpler code would break under certain conditions.

While there are definitely simpler examples in other documents, I haven't been able to find any indication that there is a simpler way to do multiprocessing and logging together, either on StackOverflow or the Python docs, other than by stumbling into it accidentally while writing code for this thread. 

Since multiprocessing in Python usually requires some extra work-arounds compared to single processing solutions, I searched specifically for multiprocessing logging options. I was also not surprised that I needed a queue, since multiprocessing is complicated. IMO the docs would be more clear if there was either a simpler example with a caveat that it wouldn't work in windows, or with a small paragraph saying that after 3.2, using logging as if it were on a single process would not cause problems.

Just to clarify, my understanding is that the child loggers on separate processes pass records to the main logger, which runs on the original process, which then handles the records and writes to a file. In this way there is no problem of simultaneously writing to the same file. Is the above correct for the code I wrote?

Thanks for your patience with my inexperience. It's been a long time since I've spent so much time on learning a new module, and I think improving the docs will help other people avoid spending so much time.
msg273553 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2016-08-24 12:53
The thing about queues does not relate just to logging - it relates to all communication between different multiprocessing processes, of which logging is just an example. The logging cookbook is not the place to provide tutorial information on e.g. multi-threading or multiprocessing and the various gotchas that can cause problems (e.g. the state of threading locks after fork). Whole books could be written about this stuff, and no doubt have been.

> Just to clarify, my understanding is that the child loggers on separate processes pass records to the main logger

No, you're mixing up loggers and handlers. Look at the basic and advanced tutorial in the stdlib docs, and e.g.

https://plumberjack.blogspot.co.uk/2009/09/python-logging-101.html

to get an idea of the difference between them. The other posts on that site are also worth looking at, though many of those posts have been incorporated into the logging cookbook over time.

> Thanks for your patience with my inexperience.

You're welcome. If further discussion is needed, it's best to continue it on e.g. Stack Overflow or the python-list mailing list.
History
Date User Action Args
2022-04-11 14:58:35adminsetgithub: 72024
2016-08-24 12:53:46vinay.sajipsetstatus: open -> closed
resolution: wont fix
messages: + msg273553

stage: resolved
2016-08-24 00:56:16yasuinikosetmessages: + msg273527
2016-08-24 00:54:39yasuinikosetfiles: - multi_log_cookbook.py
2016-08-23 16:31:56vinay.sajipsetmessages: + msg273482
2016-08-23 16:02:23yasuinikosetfiles: + mp_log_cookbook.patch
keywords: + patch
messages: + msg273474
2016-08-23 14:31:31vinay.sajipsetmessages: + msg273455
2016-08-23 13:52:56r.david.murraysetnosy: + r.david.murray, vinay.sajip
messages: + msg273447
2016-08-23 12:22:13yasuinikocreate