WindowsError:[错误5]尝试终止子进程(python)时拒绝访问

时间:2011-04-06 21:36:40

标签: 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

然后脚本死了。

引用的脚本部分:

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提供反馈!

1 个答案:

答案 0 :(得分:6)

你将你的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

即使您在显示进程正在运行的轮询后直接终止,它也可以在执行下一行之前完成。我还建议为此异常添加一个try-catch块,如果它再次发生轮询以查看该进程是否实际完成。