Python:为什么无法登录多处理
Python: why logging in multiprocessing not working
在我将我的脚本从 Mac(都是 python 2.7.*)移植到 Windows 之后,我发现所有的日志记录在子进程中都不起作用,只有 father
的日志记录被写入文件。这是我的示例代码:
# 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')
输出仅将 this is father
写入 log.out,child 的日志丢失。如何在 child 进程中启用日志记录?
每个子进程都是一个独立的进程,父进程中的文件句柄在 fork 后可能会在子进程中关闭(假设 POSIX)。在任何情况下,都不支持从多个进程记录到同一个文件。有关建议的方法,请参阅 the documentation。
在我将我的脚本从 Mac(都是 python 2.7.*)移植到 Windows 之后,我发现所有的日志记录在子进程中都不起作用,只有 father
的日志记录被写入文件。这是我的示例代码:
# 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')
输出仅将 this is father
写入 log.out,child 的日志丢失。如何在 child 进程中启用日志记录?
每个子进程都是一个独立的进程,父进程中的文件句柄在 fork 后可能会在子进程中关闭(假设 POSIX)。在任何情况下,都不支持从多个进程记录到同一个文件。有关建议的方法,请参阅 the documentation。