簡體   English   中英

Python 2.7多處理日志記錄和循環

[英]Python 2.7 Multiprocessing logging and loops

如何將我的兩個進程僅登錄到一個文件中? 使用我的代碼,只有proc1記錄到我的日志文件中... 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()

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

“盡管日志記錄是線程安全的,並且支持在單個進程中從多個線程向單個文件進行日志記錄,但不支持從多個進程向單個文件進行日志記錄

然后,您應該找到另一種方法來實現它,即實現日志服務器:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM