简体   繁体   English

将多个并行进程的输出重定向到日志文件和stdout,而无需等待进程终止

[英]Redirect the output of multiple parallel processes to both a log file and stdout without waiting until the processes terminate

I want to redirect the output of (potentially multiple) processes started from within a python script to both stdout and a log file, pretty much like as the unix tool tee does it. 我想将从python脚本开始的(可能有多个)进程的输出重定向到stdout和日志文件,就像unix工具tee一样。 A the processes are rather long-lasting I would like to show/log the line when it is printed. A该过程相当持久,我想在打印时显示/记录该行。 Line buffering is ok but waiting for the process to finish before anything is printed to stdout is not an option. 行缓冲是可以的,但是在将任何内容打印到stdout之前等待该过程完成是不可行的。

I cannot find a way to achieve that though. 不过,我找不到实现该目标的方法。 When looking at subprocess.Popen() I see that I can forward the output from stdout to a pipe, to a file descriptor or to a file object. 查看subprocess.Popen()我看到可以将输出从stdout转发到管道,文件描述符或文件对象。 It seems that I have to wait until the process ends when calling communicate() to read from the pipe. 似乎我必须等到进程结束,再调用communicate()从管道读取数据。 I neither have a file descriptor and faking a file object does not work either as Popen seems to need a real file. 我既没有文件描述符,也没有伪造文件对象,因为Popen似乎需要真实的文件。

The only solution that I know so far is reading line-by-line from Popen as suggested in Python Popen: Write to stdout AND log file simultaneously . 到目前为止,我所知道的唯一解决方案是按照Python Popen的建议从Popen逐行读取:同时写入stdout和日志文件 However, this works for a single process only. 但是,这仅适用于单个过程。 I would like to handle multiple processes launched from the python script and all should print to stdout and into the same log. 我想处理从python脚本启动的多个进程,所有进程都应打印到stdout并放入同一日志。

I especially focused on using subprocess.Popen . 我特别专注于使用subprocess.Popen Qt's QProcess seems to be a potential solution by letting callbacks do the job but unfortunately I do not have a GUI application and I do not know how to run a Qt event loop within a linearly executed python script. 通过让回调完成工作,Qt的QProcess似乎是一个潜在的解决方案,但是不幸的是我没有GUI应用程序,而且我不知道如何在线性执行的python脚本中运行Qt事件循环。

Is there any other way to capture output to stdout in a log file and at same time having live output to stdout? 还有其他方法可以将输出捕获到日志文件中的stdout并同时将输出实时输出到stdout吗?

Thanks! 谢谢!

You could use a third-party library like sarge (disclosure: I'm the maintainer) which allows you to open multiple subprocesses, capture their output streams, read from them a line at a time and do what you want with those lines. 您可以使用第三方库,例如sarge (公开:我是维护者),该库允许您打开多个子流程,捕获其输出流,一次从它们中读取一行并使用这些行来完成您想要的事情。 You need to be aware of things like process buffering issues - have a look at this section of the docs for more information. 您需要了解诸如进程缓冲问题之类的内容-有关更多信息,请参阅文档的本部分

Update: In response to your comment - yes, using sarge you can start multiple processes (using async=True ), and then you have to actively poll their output streams in the parent process. 更新:针对您的评论-是的,使用sarge您可以启动多个进程(使用async=True ),然后您必须在父进程中主动轮询其输出流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM