Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...