WindowsError:[错误5]尝试杀死子进程(python)时,访问被拒绝 [英] WindowsError: [Error 5] Access is denied when trying to kill a subprocess (python)
问题描述
所以我有一个python脚本,它运行一个循环,在该循环中它通过子进程调用程序A.Popen等待其输出,然后保存输出,然后再次调用,依此类推. (在我设置为输入的多次运行中,这种情况一直发生)
So I have a python script that runs a loop in which it calls a program A through subprocess.Popen waits for its output, then saves the output and then calls it again and so on. (This keeps happening for a number of runs I set as an input)
问题是我有一个计时器,以便每当程序A花费的时间超过特定的threshold_time时,脚本就会使用process.kill()杀死该进程并继续进行下一个迭代.
The thing is that I have a timer so that whenever the program A takes more than a particular threshold_time, the script kills the process with process.kill() and moves on to the next iteration.
问题是,即使运行300次后,一切似乎都可以正常工作,但有时还是会出现此错误:
The problem is that even though everything seems to work fine even for 300 runs, sometimes I get this error:
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
您有什么建议可能是什么问题?
Do you have any suggestions what the problem might be?
接受答案并修复代码.感谢@jedislight的反馈!
Accepted answer and fixed the code. Thanks @jedislight for your feedback!
推荐答案
您正在将p.poll()和p.kill()分开20秒.到那时,该过程可能已经完成.我建议移动time.sleep(20)调用,以便您轮询并杀死在同一时间范围内发生的事件,以避免杀死死进程.以下是在iPython中运行的示例,该示例在杀死已完成的进程时显示了类似的错误:
You are seperating your p.poll() and your p.kill() by 20 seconds. By then the process could have finished. I would suggest moving the time.sleep(20) call around so that you poll and kill happen in the same time frame, to avoid killing a dead process. Below is an example run in iPython showing a similar error when killing a completed process:
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
即使您在显示进程正在运行的民意测验之后直接杀死,它也可以在执行下一行之前完成.我还建议为此异常添加一个try-catch块,如果再次发生,则再次轮询以查看该过程是否真正完成.
Even if you kill directly after a poll that shows the processes is running it can finish before the next line is executed. I would also suggest adding a try-catch block for this exception and if it occurs poll again to see if the process actually completed.
这篇关于WindowsError:[错误5]尝试杀死子进程(python)时,访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!