Goku
Goku

Reputation: 1840

Python 2.7 Multiprocessing logging and loops

How can I put my two processes to log in a only file? With my code only proc1 is logging to my log file... module.py:

import multiprocessing,logging

log = multiprocessing.log_to_stderr()
log.setLevel(logging.DEBUG)
handler = logging.FileHandler('/var/log/my.log')
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
log.addHandler(handler)

def proc1():
    log.info('Hi from proc1')
    while True:
        if something:
            log.info('something')

def proc2():
    log.info('Hi from proc2')
    while True:
        if something_more:
             log.info('something more')

if __name__ == '__main__':
    p1 = multiprocessing.Process(target=proc1)
    p2 = multiprocessing.Process(target=proc2)
    p1.start()
    p2.start()

Upvotes: 1

Views: 743

Answers (1)

chespinoza
chespinoza

Reputation: 2658

As said at https://docs.python.org/2/howto/logging-cookbook.html#logging-to-a-single-file-from-multiple-processes

"Although logging is thread-safe, and logging to a single file from multiple threads in a single process is supported, logging to a single file from multiple processes is not supported"

Then, you should find another approach to get it, ie implementing a logging server:

https://docs.python.org/2/howto/logging-cookbook.html#sending-and-receiving-logging-events-across-a-network

Upvotes: 2

Related Questions