【发布时间】:2016-07-15 01:11:32
【问题描述】:
我有一个 Logger 类,具有以下内容:
class Logger():
def __init__(self):
self.terminal = sys.__stdout___
self.log = open('logFile.log', 'w')
def write(message):
self.terminal.write(message)
self.log.write(message)
具有以下内容的主要内容:
import Logger, sys
sys.stdout = Logger.Logger()
import File1
File1.aFunc()
在文件 1 中:
def execSubProc(target,queue):
target(queue)
q = multiprocess.Queue()
proc = multiprocess.Process(target=execSubProc, args=(testFunc,q))
proc.start()
proc.join()
在文件 2 中:
def testFunc(queue)
#Some print statements
#I want to get these print statements in the queue for main process to read
好的,这就是我的结构。我想要做的是从运行 testFunc() 的子进程中获取标准输出并将其放入队列中。当程序返回主程序时,我想从队列中读取并将这些内容写入日志文件。
我不能只在文件 2 中再次执行 sys.stdout = Logger.Logger(),因为这只会覆盖电源日志文件。我该怎么做才能捕获所有子进程标准输出并放入队列?
【问题讨论】:
标签: python logging multiprocessing stdout