有没有办法使用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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
654 次 |
| 最近记录: |