我想知道用户最近打开应用程序的时间,例如,如果打开了“notepad.exe”,则脚本应该能够检测到它最近被打开,而不必一遍又一遍地迭代所有进程。
def checkIfApplicationOpened():
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['name'])
if(pinfo['name'] == 'notepad.exe'):
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
return False
return False
while True:
print(checkIfApplicationOpened())
目前,这是我的方法,但效率非常低。请问是否有更好的方法?谢谢提前。