You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
incorrect and inconsistent logging in multiprocessing #48551
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2009-01-18.21:13:15.751>created_at=<Date2008-11-11.19:50:53.205>labels= ['type-bug', 'library']
title='incorrect and inconsistent logging in multiprocessing'updated_at=<Date2009-01-18.21:13:15.750>user='https://bugs.python.org/dursobr'
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:
importmultiprocessing, loggingdeff():
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:
importmultiprocessing, loggingdeff():
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:
importmultiprocessing, loggingdeff():
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.
(and some info about %(processName)s into the docstring) to make it
usable for me.
After that the following works like intended:
importmultiprocessingimportlogginglogging.basicConfig(level=logging.DEBUG, format='%(asctime)s%(processName)s%(threadName)s%(levelname)s%(message)s')
# omit next line to get only what your code is logginglogger=multiprocessing.get_logger()
deff():
logging.info('info from subprocess')
if__name__=='__main__':
logging.info('info from main process')
p=multiprocessing.Process(target=f)
p.start()
p.join()
Surely no perfect solution, but the monkey-patching in
multiprocessing.util._check_logger_class() dosn't seem to do the trick.
Other option is to not use %(processName)s in logging format, but
%(process)s instead - then it works without patching the logging module
(but you get numeric process IDs):
importmultiprocessingimportlogginglogging.basicConfig(level=logging.DEBUG, format='%(asctime)s%(process)s%(threadName)s%(levelname)s%(message)s')
# omit next line to get only what your code is logginglogger=multiprocessing.get_logger()
deff():
logging.info('info from subprocess')
if__name__=='__main__':
logging.info('info from main process')
p=multiprocessing.Process(target=f)
p.start()
p.join()
2009-01-05 23:48:02,046 4000 MainThread INFO info from main process
2009-01-05 23:48:02,187 3968 MainThread INFO child process calling
self.run()
2009-01-05 23:48:02,187 3968 MainThread INFO info from subprocess
2009-01-05 23:48:02,187 3968 MainThread INFO process shutting down
2009-01-05 23:48:02,187 3968 MainThread DEBUG running all "atexit"
finalizers with priority >= 0
2009-01-05 23:48:02,187 3968 MainThread DEBUG running the remaining
"atexit" finalizers
2009-01-05 23:48:02,203 3968 MainThread INFO process exiting with
exitcode 0
2009-01-05 23:48:02,203 4000 Dummy-1 INFO process shutting down
2009-01-05 23:48:02,203 4000 Dummy-1 DEBUG running all "atexit"
finalizers with priority >= 0
2009-01-05 23:48:02,203 4000 Dummy-1 DEBUG running the remaining
"atexit" finalizers
jnoller commented on Jan 18, 2009
jnollermannequin
Mannequin
More actions
This is a patch to fix the logging module, and remove the crufty code in
multiprocessing.
jnoller commented on Jan 18, 2009
jnollermannequin
Mannequin
More actions
remove the import, not needed. switch to the property
Activity
dursobr commentedon Nov 11, 2008
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:
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:
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:
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.
GregorKopka commentedon Jan 5, 2009
I ran into this shortly but was able to overcome it by patching
/Lib/logging/init.py:
43-44:
import threading
+ import multiprocessing
except ImportError:
270-271:
self.process = os.getpid()
+ self.processName = multiprocessing.current_process()._name
else:
(and some info about %(processName)s into the docstring) to make it
usable for me.
After that the following works like intended:
Surely no perfect solution, but the monkey-patching in
multiprocessing.util._check_logger_class() dosn't seem to do the trick.
Other option is to not use %(processName)s in logging format, but
%(process)s instead - then it works without patching the logging module
(but you get numeric process IDs):
2009-01-05 23:48:02,046 4000 MainThread INFO info from main process
2009-01-05 23:48:02,187 3968 MainThread INFO child process calling
self.run()
2009-01-05 23:48:02,187 3968 MainThread INFO info from subprocess
2009-01-05 23:48:02,187 3968 MainThread INFO process shutting down
2009-01-05 23:48:02,187 3968 MainThread DEBUG running all "atexit"
finalizers with priority >= 0
2009-01-05 23:48:02,187 3968 MainThread DEBUG running the remaining
"atexit" finalizers
2009-01-05 23:48:02,203 3968 MainThread INFO process exiting with
exitcode 0
2009-01-05 23:48:02,203 4000 Dummy-1 INFO process shutting down
2009-01-05 23:48:02,203 4000 Dummy-1 DEBUG running all "atexit"
finalizers with priority >= 0
2009-01-05 23:48:02,203 4000 Dummy-1 DEBUG running the remaining
"atexit" finalizers
jnoller commentedon Jan 18, 2009
This is a patch to fix the logging module, and remove the crufty code in
multiprocessing.
jnoller commentedon Jan 18, 2009
remove the import, not needed. switch to the property
jnoller commentedon Jan 18, 2009
fixed in r68737, merged to py3k in 68740