Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 636
Joseph Turian Avatar asked May 06 '11 16:05

Joseph Turian


People also ask

How do I check the operating system in Python?

The platform. uname() method in python is used to get information about the current operating system.

How do I use IDLE in Python?

To execute a file in IDLE, simply press the F5 key on your keyboard. You can also select Run → Run Module from the menu bar. Either option will restart the Python interpreter and then run the code that you've written with a fresh interpreter.

How do you check if OS is Windows or Linux in Python?

system ? system() Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'. An empty string is returned if the value cannot be determined.

How do I open Python IDLE?

To open Idle with an initial file to edit, select the Python file in an operating system window, right click (Windows) or control-click (Mac), to get a pop-up window to select how to open the file. On Windows, the line for Idle requires you to open a sub-menu. Select Idle for the latest version.


2 Answers

Unixy solution using X11/XScreenSaver to get idle time:

#!/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 X11 idle time and focused window in Python.)

Edit A cleaning up section added in the code above so that it can be periodically called from a function, because if no cleaning is supplied then an error may occur on the xDisplay due to not closed display as can be found on this link

like image 125
peth Avatar answered Sep 20 '22 05:09

peth


I guess that you are concerned about the network activity of the file transfer getting in the way of the interactive user. You don't need to worry about whether or not the user is typing on the keyboard. Really all that matters is whether or not there are competing network activities.

On Windows, for example, you can use Background Intelligent Transfer Service. This is the same service that Windows Update uses to deliver updates to your desktop without getting in the way of your use of the machine. To script it you might consider Powershell. If you are dead set on using Python you can do it with win32com.bits.

Other platforms will, no doubt, have similar offerings.

like image 31
David Heffernan Avatar answered Sep 21 '22 05:09

David Heffernan