In Python on Unix, determine if I use my computer? or standby time?

I would like to write a script to do a heavy network load in the background. However, I would like it to pause when I use my computer (either by detecting network activity, or keyboard activity, or I'm not idle).

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

+7
source share
5 answers

Unixy solution using X11 / XScreenSaver to get downtime:

#!/usr/bin/python import ctypes import os class XScreenSaverInfo( ctypes.Structure): """ typedef struct { ... } XScreenSaverInfo; """ _fields_ = [('window', ctypes.c_ulong), # screen saver window ('state', ctypes.c_int), # off,on,disabled ('kind', ctypes.c_int), # blanked,internal,external ('since', ctypes.c_ulong), # milliseconds ('idle', ctypes.c_ulong), # milliseconds ('event_mask', ctypes.c_ulong)] # events xlib = ctypes.cdll.LoadLibrary('libX11.so') display = xlib.XOpenDisplay(os.environ['DISPLAY']) xss = ctypes.cdll.LoadLibrary('libXss.so.1') xss.XScreenSaverAllocInfo.restype = ctypes.POINTER(XScreenSaverInfo) xssinfo = xss.XScreenSaverAllocInfo() xss.XScreenSaverQueryInfo(display, xlib.XDefaultRootWindow(display), xssinfo) idletime = xssinfo.contents.idle # Cleaning Up xss.XFree(xssinfo) xss.XCloseDisplay(display) print "idle: %d ms" % idletime 

(See the X11 downtime and focused window in Python .)

Edit The "Cleaning" section has been added to the above code, so it can be called from the function periodically, because if cleaning is not provided, an error may occur in xDisplay due to the non-closed display, which can be found on this link

+9
source

I assume that you are concerned about network file transfer activity interfering with the interactive user. You do not need to worry about whether the user types on the keyboard. In fact, all that matters is the presence or absence of competing network activities.

On Windows, for example, you can use the Background Intelligent Transfer Service . This is the same service that Windows Update uses to deliver updates to your desktop without interfering with your use of the machine. For a script, you can consider Powershell . If you are configured to use Python, you can do this with win32com.bits .

Other platforms will no doubt have similar offers.

+2
source

Most Linux distributions ship with ConsoleKit, which provides some session information on DBus, including "idle hint" ; this works for both X11 and text inputs.

(Nevertheless, plans to depreciate ConsoleKit by moving parts of it to systemd, the future for the idle hint hasn function has not yet been decided.)


For completeness only, os.stat(ttydev).st_mtime or os.fstat(1).st_mtime returns the last input time for tty / pty devices.

+1
source

Insert a webcam on your computer that captures the image every five seconds, and then some python modules for image analysis, which can check if you are all sitting in place.

Or plug the microswitch into your chair, plug it into the serial port of your PC (or one of these modern USB ports) and read it with Python ...

0
source

import this script. When another application, such as firefox, begins to capture bandwidth, stop the main program. There was a simple addition, but other than that it was pretty easy

0
source

All Articles