简体   繁体   English

使用 psutil 的 Python 程序在运行 8 小时后出现 PermissionError 失败:[WinError 5] 访问被拒绝 - 为什么?

[英]Python program with psutil fails after 8 hours of running with PermissionError: [WinError 5] Access is denied - why?

I have a Python 3.x program running in Windows that uses psutil.我有一个 Python 3.x 程序在使用 psutil 的 Windows 中运行。 The job of this program is simple - to periodically check if a different program is running.这个程序的工作很简单——定期检查是否有不同的程序正在运行。 If it finds the program, it does nothing.如果它找到程序,它什么也不做。 If it doesn't find the program, it starts the program.如果它没有找到该程序,它将启动该程序。 Then the program sleeps and does the same thing again in a loop.然后程序休眠并在循环中再次执行相同的操作。 Here's a minimal version of the code:这是代码的最小版本:

import psutil, os, time

found = 0

theprogram = 'hiimaprogram.exe'

while True:

    for process in psutil.process_iter():
        if process.name() == theprogram:                   # this is line 14
            found = 1
            print ('process found, doing nothing')
            

    if found == 0:
        print ('process not found, starting the process')
        filepath = theprogram
        os.startfile(filepath)
        
    found = 0

    time.sleep(10)
    print ('sleeping')
    continue

The code works great and does the job nicely.代码工作得很好,做得很好。 However, it also fails randomly, never straight away, usually at the 8 hour mark although sometimes it'll survive just a little longer.然而,它也随机失败,从不立即失败,通常在 8 小时大关,尽管有时它会存活更长时间。 Here's the traceback:这是回溯:

Traceback (most recent call last):
  File "site-packages\psutil\_pswindows.py", line 707, in wrapper
  File "site-packages\psutil\_pswindows.py", line 791, in exe
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "site-packages\psutil\_pswindows.py", line 782, in name
  File "site-packages\psutil\_pswindows.py", line 709, in wrapper
psutil.AccessDenied: psutil.AccessDenied (pid=31216)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "site-packages\psutil\_pswindows.py", line 707, in wrapper
  File "site-packages\psutil\_pswindows.py", line 784, in name
ProcessLookupError: [Errno 3] No such process

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "programchecker.py", line 14, in <module>
  File "site-packages\psutil\__init__.py", line 731, in name
  File "site-packages\psutil\_pswindows.py", line 709, in wrapper
psutil.NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=31216)

Line 14 where the process dies is marked in the code.代码中标记了进程终止的第 14 行。 Easy enough for me to write a try/except to ignore the error, but I'd really like to know why it is happening.很容易让我写一个 try/except 来忽略错误,但我真的很想知道它为什么会发生。 Try/except code:尝试/排除代码:

while True:

    for process in psutil.process_iter():
        try:
            if process.name() == theprogram:                  # this is line 14
                found = 1
                print ('process found')
        except:
            print ('error')
            time.sleep(100)
            continue

Reading up on psutil and other people who have had the same error (but for different reasons) all I can find that seems to fit my situation is that I need to be in administrator mode, but running the program in administrator mode appears to make no difference at all and still shows the same error.阅读 psutil 和其他有相同错误(但原因不同)的人,我发现似乎适合我的情况是我需要处于管理员模式,但以管理员模式运行程序似乎没有完全不同,仍然显示相同的错误。 The other thing I thought of is perhaps some automatic windows process is interfering at the eight hour mark due to sleep/hibernation but I've made sure that the computer is on an "always on" power scheme where nothing should be interfering (at least in theory).我想到的另一件事可能是由于睡眠/休眠,一些自动 windows 进程在八小时标记处受到干扰,但我确保计算机处于“始终开启”电源方案,没有任何东西应该受到干扰(至少理论上)。 Any help appreciated, thank you!任何帮助表示赞赏,谢谢!

I found the solution to this, which is completely ludicrous, but here it is.我找到了解决方案,这完全是荒谬的,但就是这样。

The actual program that my program is checking for the existence of, has a "security feature" built into it where it force-times itself out after the 8 hour mark, even if the program is active and being used .我的程序正在检查是否存在的实际程序具有内置的“安全功能”,即使该程序处于活动状态并且正在使用,它也会在 8 小时标记后强制超时。 This is direct from the devs of that program.这是直接来自该程序的开发人员。 Insane, I know.疯了,我知道。 Adding the try/except above fixes the issue, the sleep timeout of 100 is sufficient window for the master program to stop caring and reset the clock.添加上面的 try/except 解决了这个问题,100 的睡眠超时足以让主程序停止关心并重置时钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM