私は次のようにLoggerクラスを持っている:子プロセスがキューにstdoutを渡すにはどうしたらいいですか? Pythonの
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)
次メイン:File1の中
import Logger, sys
sys.stdout = Logger.Logger()
import File1
File1.aFunc()
:File2の中
def execSubProc(target,queue):
target(queue)
q = multiprocess.Queue()
proc = multiprocess.Process(target=execSubProc, args=(testFunc,q))
proc.start()
proc.join()
:
def testFunc(queue)
#Some print statements
#I want to get these print statements in the queue for main process to read
さて、ここに私の構造があります。私がやろうとしているのは、testFunc()を実行している子プロセスからstdoutを取得し、それをキューに入れることです。プログラムがmainに戻ると、私はキューから読み込み、その内容をログファイルに書きたいと思う。
sys.stdout = Logger.Logger()をファイル2にもう一度実行することはできません。これは、mainsログファイルを上書きするだけだからです。 stdoutのすべての子プロセスをキャプチャしてキューに入れるにはどうすればよいですか?