简体   繁体   English

使用Python访问已运行的进程

[英]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: 背景:

  • OS: Windows 7 64-bit 作业系统:Windows 7 64-bit

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

More discussion here 这里有更多讨论

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

相关问题 Python-将参数传递给已经运行的进程 - Python - Passing arguments to an already running process 查看python进程是否已在运行的最佳方法是什么? - What is the best way to see if a python process is already running? 如何与 Python 中已经运行的 Linux 进程通信? - How can I communicate with an already running Linux process in Python? 如何将“QUIT”命令传递给已经运行的 python 子进程 - How to pass the 'QUIT' command to the already running python sub process 是否可以从另一个已经运行的进程加载 python 变量? - Is it possible to load python variable from another already running process? 将参数/字符串传递到已经运行的进程中-Python 2.7 - Passing arguments/strings into already running process - Python 2.7 通过命名管道将数据发送到另一个已经运行的 python 进程 - Send data to another already running python process via named pipe 如果进程已经在运行,Python-Flask 设置队列 - Python-Flask setting up a que if a process is already running 检测某个进程是否已在运行并与之协作 - Detect if a process is already running and collaborate with it Python:使用subprocess.Popen执行长时间运行的进程,将其杀死,并访问其输出 - Python: executing a long-running process with subprocess.Popen, killing it, and accessing to its output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM