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
Process name not propagated to logging library #143
The code you have for multiprocessing doesn't work for me. It throws a NameError that logger is not defined -- which is expected because multiprocessing uses pickle and it doesn't have an encapsulated definition of logger. multiprocess should (and does) "work" because it's looking up logger in the global dictionary (from 'main'). However, if I modify your code to pass logger, it gives results like you are printing.
import logging
import multiprocess
import multiprocessing
logging.basicConfig(format='%(processName)s-%(process)d - %(levelname)s:%(message)s', level=logging.DEBUG)
def standardlib(logger):
if not logger: logger = logging.getLogger('multiprocessing')
logger.error("Using multiprocessing")
logger.error(multiprocessing.current_process().name)
def externallib(logger):
if not logger: logger = logging.getLogger('multiprocess')
logger.error("Using multiprocess")
logger.error(multiprocess.current_process().name)
if __name__ == "__main__":
logger = None #logging.getLogger("main")
p = multiprocessing.Process(target=standardlib, name="multiprocessing", args=(logger,))
p.start()
p.join()
p = multiprocess.Process(target=externallib, name="multiprocess", args=(logger,))
p.start()
p.join()
Interestingly, regardless of whether logger = None in __main__ or the main logger is passed in, the name of the process in externalib is still MainProcess (for python 3.8 - 3.12).
I just tried again to make sure and it works on my machine (Linux Debian 11). Are you on Windows/MAC? In such case it might be due to the spawn behaviour.
Activity
mmckerns commentedon May 6, 2023
The code you have for
multiprocessingdoesn't work for me. It throws aNameErrorthatloggeris not defined -- which is expected becausemultiprocessingusespickleand it doesn't have an encapsulated definition oflogger.multiprocessshould (and does) "work" because it's looking uploggerin the global dictionary (from'main'). However, if I modify your code to passlogger, it gives results like you are printing.Interestingly, regardless of whether
logger = Nonein__main__or the main logger is passed in, the name of the process inexternalibis stillMainProcess(for python 3.8 - 3.12).noxdafox commentedon May 6, 2023
I just tried again to make sure and it works on my machine (Linux Debian 11). Are you on Windows/MAC? In such case it might be due to the spawn behaviour.
mmckerns commentedon May 7, 2023
Yes. I was testing on a mac. Spawn has an impact on the picking.
SvenSchiffner commentedon Aug 1, 2024
I'm facing the same error in python devcontainer with loguru.
haroldmeerwaldt commentedon Sep 12, 2024
I'm getting the same error on Windows.