Gal*_*ois 10 python subprocess popen
所以我有一个运行循环的python脚本,它通过subprocess.Popen调用程序A等待输出,然后保存输出,然后再次调用,依此类推.(这种情况一直发生在我设置为输入的一些运行中)
问题是我有一个计时器,这样每当程序A花费超过特定的threshold_time时,脚本就会用process.kill()杀死进程并继续进行下一次迭代.
问题是,即使300次运行似乎一切正常,但有时我会收到此错误:
File "C:\Python27\lib\subprocess.py", line 1002, in terminate
_subprocess.TerminateProcess(self._handle, 1)
WindowsError: [Error 5] Access is denied
Run Code Online (Sandbox Code Playgroud)
然后脚本死了.
引用的脚本部分:
timeout = TIME_CONST
for run in runs:
killed = False
start = time.clock()
p = subprocess.Popen("SOME.CMD", cwd=r"some_dir")
# Monitor process. If it hits threshold, kill it and go to the next run
while p.poll() is None:
time.sleep(20)
secs_passed = time.clock() - start
### the following was my initial buggy line ###
#if secs_passed >= timeout:
### corrected line after jedislight's answer ###
#if the time is over the threshold and process is still running, kill it
if secs_passed >= timeout and p.poll is None:
p.kill()
killed = True
break
if killed: continue
Run Code Online (Sandbox Code Playgroud)
您有什么建议可能是什么问题吗?
编辑: 接受的答案并修复了代码.感谢@jedislight的反馈!
小智 7
你将你的p.poll()和你的p.kill()分开20秒.到那时,这个过程已经完成了.我建议移动time.sleep(20)调用,以便在相同的时间范围内进行轮询和杀死,以避免杀死一个死进程.以下是在iPython中运行的示例,在杀死已完成的进程时显示类似的错误:
In [2]: import subprocess
In [3]: p = subprocess.Popen("dir")
In [4]: p.poll()
Out[4]: 0
In [5]: p.kill()
---------------------------------------------------------------------------
WindowsError Traceback (most recent call last)
C:\Users\---\<ipython console> in <module>()
C:\Python26\lib\subprocess.pyc in terminate(self)
947 """Terminates the process
948 """
--> 949 _subprocess.TerminateProcess(self._handle, 1)
950
951 kill = terminate
WindowsError: [Error 5] Access is denied
Run Code Online (Sandbox Code Playgroud)
即使您在显示进程正在运行的轮询之后直接终止,它也可以在执行下一行之前完成.我还建议为此异常添加一个try-catch块,如果它再次发生轮询以查看该进程是否实际完成.