简体   繁体   English

将stdout和stderr与Python popen合并并检测stderr是否为空

[英]Merge stdout and stderr with Python popen AND detect if stderr is empty

I want to run a sub process with popen that can send messages to both stdout and stderr, but that sub process will continue along it's merry way despite writing to stderr. 我想运行一个带有popen的子进程,该子进程可以将消息发送到stdout和stderr,但是尽管写了stderr,该子进程仍将沿其快乐的方式继续进行。

I want to stream stdout and stderr together so I get the output in the exact order it occurred (or was flushed I guess technically). 我想将stdout和stderr一起流式传输,因此我可以按发生的确切顺序获得输出(或者从技术上讲是刷新的)。 Then, I want to log that full result set. 然后,我要记录完整的结果集。 BUT I also want to independently know if stderr is empty. 但是我也想独立知道stderr是否为空。 If it is not, I'm going to throw an exception. 如果不是,我将抛出异常。

It is clear to me how I can get them separately, or merged together, but how can I perhaps do both? 我很清楚如何将它们分开或合并在一起,但是我又可能怎么做?

First off I solved my actual use case in large part by digging further into the process producing the streams. 首先,我进一步深入研究了生成流的过程,从而解决了我的实际用例。 I was using the MySQL client, and found the right combination of command line switches would solve the real goals. 我使用的是MySQL客户端,发现正确的命令行开关组合可以解决真正的目标。 I'll leave that out since it's entirely specific to that situation. 我将其排除在外,因为它完全是针对这种情况的。

Before I came upon that, however, I did find a very useful direction to explore - the use of the program "Tee" and / or a Python source equivalent. 但是,在此之前,我确实找到了一个非常有用的探索方向-使用程序“ Tee”和/或等效的Python源代码。 I did not try to implement it, but is seems like a method one could use to simultaneously collect the output from both stdout and stderr, while sending just stdout to another file descriptor. 我没有尝试实现它,但是似乎可以同时从stdout和stderr收集输出的一种方法,同时仅将stdout发送到另一个文件描述符。 If the two outputs are not the same, then that would tell you if stderr was utilized. 如果两个输出不相同,则将告诉您是否使用了stderr。

Check out : 查看 :

https://www.computerhope.com/unix/utee.htm https://www.computerhope.com/unix/utee.htm

And : 和:

Python subprocess get children's output to file and terminal? Python子进程将孩子的输出输出到文件和终端?

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

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