我有一个名为BOB.exe的可执行函数,它只用短暂的暂停将标准量的文本输出到stdout。 BOB也有冻结的习惯,所以我编写了一个python监视器函数,它使用子进程模块调用BOB可执行文件,将其指向临时文件并监视临时文件的大小以查看它是否已崩溃。这是我目前的解决方案:Python:读取子进程的stdout而不打印到文件
#!/usr/bin/python
from subprocess import Popen
import tempfile, time
def runBOB(argsList):
# Create a temporary file where BOB stdout will be piped
BOBout = tempfile.NamedTemporaryFile()
BOBoutSize = 0
# Start the subprocess of BOB
BOBsp = Popen(argsList, stdout=BOBout)
while True:
# See if subprocess has finished
if BOBsp.poll() is not None:
BOBout.close() # Destroy the temp file
return 0
# if the size of the stdout file has increased, BOB.exe is still running
BOBoutSizeNew = os.path.getsize(BOBout.name)
if BOBoutSizeNew > BOBoutSize:
BOBoutSize = BOBoutSizeNew
else: # if not, kill it
BOBsp.kill()
BOBout.close() # Destroy the temp file
return 1
# Check every 10 seconds
time.sleep(10)
但是,这是非常慢,我认为写入文件是原因。有没有更高效的方法来做到这一点,比如观看标准输出流,然后立即将其发送给Null?任何减少打印兆字节数的内容都可能会有所帮助。是否有另一种方法来查看exe是否崩溃?我应该注意到,我不在乎stdout,反正这一切都将被忽略
谢谢你的帮助!
非常感谢您为!也很容易理解。一个问题,因为我不太熟悉pythons线程:'readBOB'会在后台不断处理,或者循环运行的频率有一定限制吗?出于效率的原因,这是明智的吗? – FHTMitchell
@Goose'readBOB'不是忙碌循环,如果这就是你所担心的。当没有数据要读时,'os.read'将会被阻塞,所以'readBOB'只会花费尽可能多的CPU来读取(和丢弃)Bob产生的1k块数据,这应该是可以忽略的。 – user4815162342