Skip to content
Advertisement

In Python on Unix, determine if I am using my computer? or idle?

I would like to write a script to do an heavy network upload, in the background. However, I would like it to pause when I am using my computer (either by detecting network activity or keyboard activity or that I am not idle).

What is the best way to detect that I am using the computer, on Python on Unix?

Advertisement

Answer

Unixy solution using X11/XScreenSaver to get idle time:

JavaScript

(From “X11 idle time and focused window in Python”, originally found on thp.io, now apparently only the GitHub gist by the same author survives.)

A cleanup section was added to the code in a later edit by another user so that it can be called periodically.

As noted in a comment to the answer they reference, note that you should also do proper return code checking on function calls to avoid ungraceful program termination when X display and other initializations fail for some reason.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement