Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process name not propagated to logging library #143

Open
noxdafox opened this issue May 5, 2023 · 4 comments
Open

Process name not propagated to logging library #143

noxdafox opened this issue May 5, 2023 · 4 comments

Comments

@noxdafox
Copy link

noxdafox commented May 5, 2023

Greetings,

When investigating the following bug:
noxdafox/pebble#113

It turned out the multiprocess library seems to not propagate the process name to the logging library correctly.

import logging
import multiprocess
import multiprocessing


logging.basicConfig(format='%(processName)s-%(process)d - %(levelname)s:%(message)s', level=logging.DEBUG)


def standardlib():
    logger.error("Using multiprocessing")
    logger.error(multiprocessing.current_process().name)


def externallib():
    logger.error("Using multiprocess")
    logger.error(multiprocess.current_process().name)


if __name__ == "__main__":
    logger = logging.getLogger("main")

    p = multiprocessing.Process(target=standardlib, name="multiprocessing")
    p.start()
    p.join()

    p = multiprocess.Process(target=externallib, name="multiprocess")
    p.start()
    p.join()

Output:

multiprocessing-7814 - ERROR:Using multiprocessing
multiprocessing-7814 - ERROR:multiprocessing
MainProcess-7815 - ERROR:Using multiprocess
MainProcess-7815 - ERROR:multiprocess

The expected output would be:

multiprocessing-7814 - ERROR:Using multiprocessing
multiprocessing-7814 - ERROR:multiprocessing
multiprocess-7815 - ERROR:Using multiprocess
multiprocess-7815 - ERROR:multiprocess
$ python --version
Python 3.9.2
@mmckerns
Copy link
Member

mmckerns commented May 6, 2023

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).

@noxdafox
Copy link
Author

noxdafox commented 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
Copy link
Member

mmckerns commented May 7, 2023

Yes. I was testing on a mac. Spawn has an impact on the picking.

@SvenSchiffner
Copy link

I'm facing the same error in python devcontainer with loguru.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants