Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

Python: why logging in multiprocessing not working

After I port my script to Windows from Mac (both python 2.7.*), I find that all the logging not working in subprocess, only the father's logging are write to file. Here is my example code:

# test log among multiple process env
import logging, os
from multiprocessing import Process

def child():
    logging.info('this is child')

if __name__ == '__main__':
    logging.basicConfig(filename=os.path.join(os.getcwd(), 'log.out'),
            level = logging.DEBUG, filemode='w',
            format = '[%(filename)s:%(lineno)d]: %(asctime)s - %(levelname)s: %(message)s')


    p = Process(target = child, args = ())
    p.start()
    p.join()

    logging.info('this is father')

the output only write this is father into log.out, and the child's log missing. How to make logging woking in child process?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Each child is an independent process, and file handles in the parent may be closed in the child after a fork (assuming POSIX). In any case, logging to the same file from multiple processes is not supported. See the documentation for suggested approaches.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...