Python 监控子进程的 stderr 和 stdout
我试图从 python 2.7 中启动一个程序(HandBreakCLI)作为子进程或线程。我已经启动了它,但我不知道如何监视它的 stderr 和 stdout。
程序将其状态(完成百分比)和有关编码的信息分别输出到 stderr 和 stdout。我希望能够定期从适当的流中检索完成的百分比。
我尝试使用 stderr 和 stdout 设置为 PIPE 来调用 subprocess.Popen 并使用 subprocess.communicate,但它会等待进程被终止或完成,然后检索输出。对我没有多大好处。
我已经将它启动并作为线程运行,但据我所知,我仍然必须最终调用 subprocess.Popen 来执行程序并遇到同一堵墙。
我以正确的方式处理这件事吗?我还有哪些其他选择或如何使其按照描述的方式工作?
I trying to start a program (HandBreakCLI) as a subprocess or thread from within python 2.7. I have gotten as far as starting it, but I can't figure out how to monitor it's stderr and stdout.
The program outputs it's status (% done) and info about the encode to stderr and stdout, respectively. I'd like to be able to periodically retrieve the % done from the appropriate stream.
I've tried calling subprocess.Popen with stderr and stdout set to PIPE and using the subprocess.communicate, but it sits and waits till the process is killed or complete then retrieves the output then. Doesn't do me much good.
I've got it up and running as a thread, but as far as I can tell I still have to eventually call subprocess.Popen to execute the program and run into the same wall.
Am I going about this the right way? What other options do I have or how to I get this to work as described?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用 ffmpeg 完成了同样的任务。这是相关部分的精简版本。
bufsize=1
表示行缓冲,可能不需要。编辑1:我注意到子进程(在本例中为手刹)需要在行后刷新才能使用它(ffmpeg 会这样做)。
编辑 2:一些快速测试表明,实际上可能并不需要
bufsize=1
。I have accomplished the same with ffmpeg. This is a stripped down version of the relevant portions.
bufsize=1
means line buffering and may not be needed.Edit 1: I noticed that the subprocess (handbrake in this case) needs to flush after lines to use this (ffmpeg does).
Edit 2: Some quick tests reveal that
bufsize=1
may not be actually needed.