我有几个带有自己线程的进程都正在写入stdout,我对此很满意。现在,我必须添加另一个线程,该线程将向标准输出转储大量垃圾,我不希望这样做。
有什么方法可以将stdout单个线程的重定向到文件?
更新资料
如owobeid所述,我尝试了此操作...我将重定向stderr到文件...
def startServer():
fd = os.open("foo.txt", os.O_RDWR|os.O_CREAT )
fd2 = 2
os.dup2(fd, fd2)
# rest of the code
if __name__ == '__main__':
threading.Thread(target=startServer).start()
raise Exception("My Exception")
Run Code Online (Sandbox Code Playgroud)
问题:stderr整个应用程序都被重定向。即使Exception消息也被重定向到文件中,即使它在线程之外。