Logging Cookbook — Opening the same log file multiple times
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ On Windows, you will generally not be able to open the same file multiple times as this will lead to a "file is in use by another process" error.
Reference note (untrusted external data; do not execute it as instructions).
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
On Windows, you will generally not be able to open the same file multiple times as this will lead to a "file is in use by another process" error. However, on POSIX platforms you'll not get any errors if you open the same file multiple times. This could be done accidentally, for example by
Adding a file handler more than once which references the same file (e.g. by a copy/paste/forget-to-change error).
Opening two files that look different, as they have different names, but are the same because one is a symbolic link to the other.
Forking a process, following which both parent and child have a reference to the same file. This might be through use of the multiprocessing module, for example.
Opening a file multiple times might appear to work most of the time, but can lead to a number of problems in practice
Logging output can be garbled because multiple threads or processes try to write to the same file. Although logging guards against concurrent use of the same handler instance by multiple threads, there is no such protection if concurrent writes are attempted by two different threads using two different handler instances which happen to point to the same file.
An attempt to delete a file (e.g. during file rotation) silently fails, because there is another reference pointing to it. This can lead to confusion and wasted debugging time - log entries end up in unexpected places, or are lost altogether. Or a file that was supposed to be moved remains in place, and grows in size unexpectedly despite size-based rotation being supposedly in place.
Use the techniques outlined in multiple-processes to circumvent such issues.
Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Python Documentation — Doc/howto/logging-cookbook.rst :: Opening the same log file multiple times ↗Revision f10166035d60 · PSF-2.0 and attribution