所以我有一个python脚本运行一个循环,在其中它通过subprocess调用程序A.Popen等待它的输出,然后保存输出,然后再次调用,等等。 (这一直发生在我设置为输入的多次运行中)WindowsError:[错误5]试图杀死一个子进程时,访问被拒绝(python)
事情是,我有一个计时器,以便每当程序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
,然后脚本死亡。
的简称脚本部分:
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
你有任何建议,问题可能是什么?
编辑: 接受的答案并修复了代码。感谢@ jedislight的反馈!
你能发布可以重现此行为的最小Python脚本吗? – 2011-04-07 00:13:32
@Sridhar我添加了你要求的代码。 – Galois 2011-04-08 03:11:26
我试着用“SOME.CMD”只是“CMD”,它的工作。你想要杀死的是什么过程? – Fenikso 2011-04-13 07:27:44