【发布时间】:2011-10-23 15:32:51
【问题描述】:
我有一个程序P1,我需要使用不同的输入运行大约 24*20000 次。问题是P1 挂起,我应该手动强制它(kill)。我的第一个解决方案是编写一个 python 脚本来调用P1,并使用popen 和communicate 传递正确的输入并接收输出。但是由于等待输出的通信的性质,只要它在等待响应,我就不能杀死进程。我在 Windows 上。
我尝试使用multiprocess 函数,但它只运行P1 并且未能将输入发送给它。我怀疑在popen 中不使用管道并尝试了一点,但我想我无法接收来自P1 的输出。
有什么想法吗?
# This code run XLE and pass the intended input to it automatically
def startExe(programPath, programArgStr):
p = subprocess.Popen(programPath,stdout=subprocess.PIPE,stdin=subprocess.PIPE) p.stdin.write(programArgStr)
p.communicate()[0]
# Need to kill the process if it takes longer than it should here
def main(folder):
..
#loop
programArgStr = "create-parser"+path1+";cd "+ path2+"/s"+ command(counter) +";exit"
startExe(path, programArgStr)
..
您可以看到 P1 是否可以成功完成给定任务,它可以使用传递给它的退出命令自行退出!
【问题讨论】:
标签: python subprocess kill popen terminate