[issue4301] incorrect and inconsistent logging in multiprocessing

Brian D'Urso report at bugs.python.org
Tue Nov 11 20:50:54 CET 2008


New submission from Brian D'Urso <dursobr at pitt.edu>:

logging in multiprocessing seems to give inconsistent results on Linux
and XP, but neither appears to match the documentation:

According to the docs, when first created "the logger has level
logging.NOTSET and has a handler which sends output to sys.stderr using
format '[%(levelname)s/%(processName)s] %(message)s'" The example in the
docs also does not work, but the full problem is more clearly
illustrated with the following:

This should print a message from the main process and the subprocess:

import multiprocessing, logging

def f():
    logger = multiprocessing.get_logger()
    logger.info('info from subprocess')

if __name__ == '__main__':
    logger = multiprocessing.get_logger()
    logger.setLevel(logging.INFO)
    logger.info('info from main process')
    p = multiprocessing.Process(target=f)
    p.start()
    p.join()

but, on Windows (XP) and Linux (ScientificLinux 5.2, python 2.6 compiled
from src) it outputs only:

No handlers could be found for logger "multiprocessing"

We can 'fix' this by specifying a handler, as in:

import multiprocessing, logging

def f():
    logger = multiprocessing.get_logger()
    logger.info('info from subprocess')

if __name__ == '__main__':
    logger = multiprocessing.get_logger()
    logger.setLevel(logging.INFO)
    handler = logging.StreamHandler()
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('[%(levelname)s/%(processName)s]
%(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.info('info from main process')
    p = multiprocessing.Process(target=f)
    p.start()
    p.join()

Then on Linux I get:

[INFO/MainProcess] info from main process
[INFO/Process-1] child process calling self.run()
[INFO/Process-1] info from subprocess
[INFO/Process-1] process shutting down
[INFO/Process-1] process exiting with exitcode 0
[INFO/MainProcess] process shutting down

which looks better, but on Windows I get:

[INFO/MainProcess] info from main process
No handlers could be found for logger "multiprocessing"
[INFO/MainProcess] process shutting down

So, the logger is still not setup correctly in the subprocess on
Windows. We can again try to 'fix' this to:

import multiprocessing, logging

def f():
    logger = multiprocessing.get_logger()
    handler = logging.StreamHandler()
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('[%(levelname)s/%(processName)s]
%(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.info('info from subprocess')

if __name__ == '__main__':
    logger = multiprocessing.get_logger()
    logger.setLevel(logging.INFO)
    handler = logging.StreamHandler()
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('[%(levelname)s/%(processName)s]
%(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.info('info from main process')
    p = multiprocessing.Process(target=f)
    p.start()
    p.join()

Now, on Linux I get:

[INFO/MainProcess] info from main process
[INFO/Process-1] child process calling self.run()
[INFO/Process-1] info from subprocess
[INFO/Process-1] info from subprocess
[INFO/Process-1] process shutting down
[INFO/Process-1] process shutting down
[INFO/Process-1] process exiting with exitcode 0
[INFO/Process-1] process exiting with exitcode 0
[INFO/MainProcess] process shutting down

i.e. double output from the subprocess!
On Windows, I finally get:

[INFO/MainProcess] info from main process
No handlers could be found for logger "multiprocessing"
[INFO/Process-1] info from subprocess
[INFO/Process-1] process shutting down
[INFO/Process-1] process exiting with exitcode 0
[INFO/MainProcess] process shutting down

which is almost OK, but the logger is getting set up too late to pick up
the subprocess self.run

It seems like the logger is not being setup correctly by
multiprocessing.get_logger() and the behavior is different in a
subprocess on Windows vs Linux.

----------
components: Library (Lib)
messages: 75757
nosy: dursobr
severity: normal
status: open
title: incorrect and inconsistent logging in multiprocessing
type: behavior
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4301>
_______________________________________


More information about the Python-bugs-list mailing list