[英]Accessing an ALREADY running process, with Python
Question: Is there a way, using Python, to access the stdout of a running process ? 问题:有没有一种使用Python的方法来访问正在运行的进程的标准输出? This process has not been started by Python.
这个过程尚未被Python启动。
Context: There is a program called mayabatch , that renders out images from 3D Maya scene files. 上下文:有一个名为mayabatch的程序,该程序可从3D Maya场景文件中渲染图像。 If I were to run the program from the command line I would see progress messages from mayabatch .
如果我要从命令行运行程序,我会看到mayabatch的进度消息。 Sometimes, artists close these windows, leaving the progress untracable until the program finishes.
有时,艺术家会关闭这些窗口,直到程序完成之前,进度都无法控制。 That led me along this route of trying to read its stdout after it's been spawned by a foreign process.
这导致我沿这条路线尝试在被外部程序生成后读取其标准输出。
Background: 背景:
My research so far: I have only found questions and answers of how to do this if it was a subprocess, using the subprocess
module. 到目前为止,我的研究是:我仅使用
subprocess
模块找到了有关如何在子流程中执行此操作的问题和答案。 I also looked briefly into psutil
, but I could not find any way to read a process' stdout. 我还简要地研究了
psutil
,但是找不到任何读取进程标准输出的方法。
Any help would be really appreciated. 任何帮助将非常感激。 Thank you.
谢谢。
I don't think you can get to the stdout of a process outside of the code that created it 我认为您无法在创建该程序的代码之外获得该程序的标准输出
The lazy way to is just to pipe the output of mayabatch
to a text file, and then poll the text file periodically in your own code so it's under your control, rather than forcing you to wait on the pipe (which is especially hard on Windows, since Windows select
doesn't work with the pipes used by subprocess. 懒惰的方法是将
mayabatch
的输出通过管道mayabatch
到文本文件,然后以自己的代码定期轮询文本文件,从而使它在您的控制之下,而不是强迫您等待管道(在Windows上尤其困难) ,因为Windows select
不适用于子流程使用的管道。
I think this is what maya does internally too: by default mayaBatch logs its results to a file called mayaRenderLog.txt
in the user's Maya directory. 我认为这也是maya内部执行的操作:默认情况下,mayaBatch将其结果记录到用户的Maya目录中的名为
mayaRenderLog.txt
的文件中。
If you're running mayabatch from the command line or a bat file, you can funnel stdout to a file with a >
character: 如果从命令行或bat文件运行mayabatch,则可以将stdout漏斗到带有
>
字符的文件中:
mayabatch.exe "file.ma" > log.txt
You should be able to poll that text file from the outside using standard python as long as you only open it for reading. 只要您仅打开文件以进行阅读,就应该可以使用标准python从外部轮询该文本文件。 The advantage of doing it this way is that you control the frequency at which you check the file.
这样做的好处是您可以控制检查文件的频率。
OTOH If you're doing it from python, it's a little tougher unless you don't mind having your python script idled until the mayabatch completes. OTOH如果您是从python编写的,这会有点困难,除非您不介意在mayabatch完成之前将python脚本闲置。 The usual subprocess recipe, which uses
popen.communicate()
is going to wait for an end-of-process return code: 使用
popen.communicate()
的常规子流程配方将等待流程结束返回代码:
test = subprocess.Popen(["mayabatch.exe","filename.mb"], stdout=subprocess.PIPE)
print test.communicate()[0]
works but won't report until the process dies. 可以工作,但要等到过程终止后才能报告。 But you calling
readlines
on the process's stdout will trigger the process and report it one line at a time: 但是,您在流程的stdout上调用
readlines
将触发该流程并一次报告一行:
test = subprocess.Popen(["mayabatch.exe","filename.mb"], stdout=subprocess.PIPE)
reader = iter(test.subprocess.readlines, "")
for line in reader:
print line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.