2016-11-30 10 views
1

ログをいくつかのプロセスのコンソールに出力する方法は? 例:Pythonがマルチプロセスでログインする

import multiprocessing, logging, multiprocessing_logging 

logging.basicConfig(level=logging.INFO) 
logger = logging.getLogger() 
multiprocessing_logging.install_mp_handler(logger) 

def worker(): 
    while True: 
     logger.info("This is logging for TEST1") 

def worker2(): 
    while True: 
     logger.info("This is logging for TEST2") 

if __name__ == '__main__': 
    p1 = multiprocessing.Process(target=worker) 
    p1.daemon = True 
    p1.start() 

    p2 = multiprocessing.Process(target=worker2) 
    p2.daemon = True 
    p2.start() 

    p1.join() 
    p2.join() 

しかし、出力が正しくありません。

INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST2 
IINFO:root:This is logging for TEST1 
NFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST1 
IINFO:root:This is logging for TEST2 
NFO:root:This is logging for TEST1 
IINFO:root:This is logging for TEST2 
NFO:root:This is logging for TEST1 

私はmultiprocessing-loggingライブラリを使用しようとしましたが、それは

+0

'multiprocessing-logging'はあなたの問題を解決するはずです - あなたがそれを使用しているときに何が動作しないかを示す[MCVE]を提供できますか? –

+0

[link](https://github.com/jruere/multiprocessing-logging#usage)と書かれているように、ログを設定した後、multiprocessing-loggingをインポートしてmultiprocessing_logging.install_mp_handler()を追加してください – deniska369

+0

その場合、[fileバグ報告](https://github.com/jruere/multiprocessing-logging/issues/new)に 'multiprocessing-logging'を追加しました。 –

答えて

1

を助けていないあなたの(更新)コードはこっち正常に動作します:

INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST1 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 
INFO:root:This is logging for TEST2 

あなたのためでなければ、file a bug report with multiprocessing-logging

+0

'IINFO:root:これはTEST2のログです NFO:root:これはTEST1のログです' 私のために働きません – deniska369

+0

ありがとう、ありがとう! – deniska369

関連する問題