Get Mac idle time C or Python
How can i get system idle time (no keys pressed - mouse mo开发者_JAVA技巧ved) in C or Python?
EDIT: My program suspend a counter when idle time > 10 sec
I don't have a Mac to test on at the moment, so cannot confirm this works, but this thread seems to offer the solution you're looking for: http://www.dssw.co.uk/sleepcentre/threads/system_idle_time_how_to_retrieve.html
In a nutshell, use the subprocess module and call:
ioreg -c IOHIDSystem
Then parse the output, and divide the value by 10^9 to get the idle time in seconds.
what about that in python ( win ) ? like time module in standard lib of python ... ?
Is your application GUI-driven?
If so, you could start a timer when the GUI comes up and then have keyboard and mouse callbacks at the top level. Every time the keyboard or mouse callback is triggered, reset the timer. Otherwise when the timer goes off, it's handler code should suspend your counter.
If not, ignore this... ;-)
If you mean the time spent during the course of a program, after the program is done, then the easiest thing to do is run the unix "time" function:
# echo foo
foo
# time echo foo
foo
real 0m0.000s
user 0m0.000s
sys 0m0.000s
There's a more technical answer if you really need to be doing the data collection while the program is running - I'll take a look at the unix book for you...
Update:
clock_t times( struct tms *buf );
Look up the details on the manpage or in section 8.15 of "Advanced Programming in the UNIX Environment" - Stevens - the bible for these kinds of questions.
Update 2:
Run "sar" in another process (it's like a stdout-printing version of top), look for a result you want, and send a signal to the process.
Alternately, the other solution posted regarding "ioreg".
However, if you have a program that is itself "kernel-like" in nature, with a big action loop at its core, you may find it best to keep track of unused cycles yourself. Otherwise, this is a non-trivial problem to solve.
精彩评论