Python program with psutil fails after 8 hours of running with PermissionError: [WinError 5] Access is denied - why?
Python program with psutil fails after 8 hours of running with PermissionError: [WinError 5] Access is denied - why?
我在 Windows 中有一个 Python 3.x 程序 运行 使用 psutil。这个程序的工作很简单 - 定期检查是否有不同的程序 运行。如果它找到程序,它什么都不做。如果它没有找到该程序,它就会启动该程序。然后程序休眠并在循环中再次做同样的事情。这是代码的最小版本:
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
该代码工作得很好并且很好地完成了工作。但是,它也会随机失败,不会立即失败,通常会在 8 小时后失败,但有时它会存活更长时间。这是回溯:
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)
第14行在代码中标记了进程死亡的地方。我很容易写一个 try/except 来忽略错误,但我真的很想知道为什么会这样。 Try/except代码:
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
阅读 psutil 和其他有相同错误(但出于不同原因)的人,我发现似乎适合我的情况是我需要处于管理员模式,但是 运行管理员模式下的程序似乎没有任何区别,仍然显示相同的错误。我想到的另一件事是,由于 sleep/hibernation,某些自动 windows 进程可能会在八小时标记时受到干扰,但我已确保计算机处于“始终开启”的电源方案,其中没有任何东西应该干扰(至少在理论上)。感谢任何帮助,谢谢!
我找到了这个问题的解决方案,这完全是荒谬的,但就是这样。
我的程序正在检查其存在的实际程序具有内置的“安全功能”,它会在 8 小时标记后强制自行超时,即使该程序是活跃并被使用。这是直接来自该程序的开发者。疯了,我知道。添加上面的 try/except 解决了这个问题,100 的睡眠超时时间足以 window 主程序停止关心并重置时钟。
我在 Windows 中有一个 Python 3.x 程序 运行 使用 psutil。这个程序的工作很简单 - 定期检查是否有不同的程序 运行。如果它找到程序,它什么都不做。如果它没有找到该程序,它就会启动该程序。然后程序休眠并在循环中再次做同样的事情。这是代码的最小版本:
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
该代码工作得很好并且很好地完成了工作。但是,它也会随机失败,不会立即失败,通常会在 8 小时后失败,但有时它会存活更长时间。这是回溯:
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)
第14行在代码中标记了进程死亡的地方。我很容易写一个 try/except 来忽略错误,但我真的很想知道为什么会这样。 Try/except代码:
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
阅读 psutil 和其他有相同错误(但出于不同原因)的人,我发现似乎适合我的情况是我需要处于管理员模式,但是 运行管理员模式下的程序似乎没有任何区别,仍然显示相同的错误。我想到的另一件事是,由于 sleep/hibernation,某些自动 windows 进程可能会在八小时标记时受到干扰,但我已确保计算机处于“始终开启”的电源方案,其中没有任何东西应该干扰(至少在理论上)。感谢任何帮助,谢谢!
我找到了这个问题的解决方案,这完全是荒谬的,但就是这样。
我的程序正在检查其存在的实际程序具有内置的“安全功能”,它会在 8 小时标记后强制自行超时,即使该程序是活跃并被使用。这是直接来自该程序的开发者。疯了,我知道。添加上面的 try/except 解决了这个问题,100 的睡眠超时时间足以 window 主程序停止关心并重置时钟。