有没有办法用Python测试系统在Mac上闲置了多久?或者,即使系统目前处于空闲状态,也不能做到这一点?
答案
使用来自可接受的解决方案的信息,下面是一个丑陋但实用的、相当有效的作业函数:
from subprocess import *
def idleTime():
'''Return idle time in seconds'''
# Get the output from
# ioreg -c IOHIDSystem
s = Popen(["ioreg", "-c", "IOHIDSystem"], stdout=PIPE).communicate()[0]
lines = s.split('\n')
raw_line = ''
for line in lines:
if line.find('HIDIdleTime') > 0:
raw_line = line
break
nano_seconds = long(raw_line.split('=')[-1])
seconds = nano_seconds/10**9
return seconds发布于 2010-03-11 13:32:29
未进行测试(目前),但按照这条线可以解析
-c IOHIDSystem
https://stackoverflow.com/questions/2425087
复制相似问题