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

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...