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

incorrect and inconsistent logging in multiprocessing #48551

Closed
dursobr mannequin opened this issue Nov 11, 2008 · 5 comments
Closed

incorrect and inconsistent logging in multiprocessing #48551

dursobr mannequin opened this issue Nov 11, 2008 · 5 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@dursobr
Copy link
Mannequin

dursobr mannequin commented Nov 11, 2008

BPO 4301
Files
  • issue4301.patch
  • 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 = None
    closed_at = <Date 2009-01-18.21:13:15.751>
    created_at = <Date 2008-11-11.19:50:53.205>
    labels = ['type-bug', 'library']
    title = 'incorrect and inconsistent logging in multiprocessing'
    updated_at = <Date 2009-01-18.21:13:15.750>
    user = 'https://bugs.python.org/dursobr'

    bugs.python.org fields:

    activity = <Date 2009-01-18.21:13:15.750>
    actor = 'jnoller'
    assignee = 'jnoller'
    closed = True
    closed_date = <Date 2009-01-18.21:13:15.751>
    closer = 'jnoller'
    components = ['Library (Lib)']
    creation = <Date 2008-11-11.19:50:53.205>
    creator = 'dursobr'
    dependencies = []
    files = ['12789']
    hgrepos = []
    issue_num = 4301
    keywords = ['patch', 'needs review']
    message_count = 5.0
    messages = ['75757', '79206', '80122', '80124', '80127']
    nosy_count = 3.0
    nosy_names = ['jnoller', 'dursobr', 'Gregor_Kopka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue4301'
    versions = ['Python 2.6']

    @dursobr
    Copy link
    Mannequin Author

    dursobr mannequin commented 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:

    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.

    @dursobr dursobr mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 11, 2008
    @GregorKopka
    Copy link
    Mannequin

    GregorKopka mannequin commented 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:

    import multiprocessing
    import logging
    logging.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 logging
    logger = multiprocessing.get_logger()
    
    def f():
        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):

    import multiprocessing
    
    import logging
    logging.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 logging
    logger = multiprocessing.get_logger()
    
    def f():
        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
    Copy link
    Mannequin

    jnoller mannequin commented Jan 18, 2009

    This is a patch to fix the logging module, and remove the crufty code in
    multiprocessing.

    @jnoller
    Copy link
    Mannequin

    jnoller mannequin commented Jan 18, 2009

    remove the import, not needed. switch to the property

    @jnoller
    Copy link
    Mannequin

    jnoller mannequin commented Jan 18, 2009

    fixed in r68737, merged to py3k in 68740

    @jnoller jnoller mannequin closed this as completed Jan 18, 2009
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    0 participants