在 Unix 上的 Python 中,确定我是否正在使用我的计算机?还是闲着?

发布于 2024-11-06 00:55:13 字数 112 浏览 4 评论 0 原文

我想编写一个脚本来在后台进行大量网络上传。 但是,我希望它在我使用计算机时暂停(通过检测网络活动或键盘活动或者我不闲着)。

检测我正在使用 Unix 上的 Python 计算机的最佳方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

画骨成沙 2024-11-13 00:55:13

使用 X11/XScreenSaver 获取空闲时间的 Unixy 解决方案:(

#!/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)

print "idle: %d ms" % xssinfo.contents.idle

# cleanup
xss.XCloseDisplay(display)
xss.XFree(xssinfo)

来自“Python 中的 X11 空闲时间和聚焦窗口”,最初位于 thp.io,现在显然只有 GitHub要点仍然存在。)

清理部分在后来的编辑中添加到代码中由另一个用户 以便可以定期调用它。

正如他们引用的答案的评论中所指出的,请注意,您还应该对函数调用进行正确的返回代码检查,以避免当 X 显示和其他初始化由于某种原因失败时程序异常终止。

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)

print "idle: %d ms" % xssinfo.contents.idle

# cleanup
xss.XCloseDisplay(display)
xss.XFree(xssinfo)

(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.

情归归情 2024-11-13 00:55:13

我猜您担心文件传输的网络活动会妨碍交互式用户。您无需担心用户是否正在键盘上打字。真正重要的是是否存在竞争性网络活动。

例如,在 Windows 上,您可以使用 后台智能接送服务。 Windows 更新也使用此服务向您的桌面提供更新,而不会妨碍您使用计算机。要编写脚本,您可以考虑 Powershell 。如果您执意要使用 Python,则可以使用 win32com.bits 来实现。

毫无疑问,其他平台也会提供类似的产品。

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.

拥抱我好吗 2024-11-13 00:55:13

大多数 Linux 发行版都附带 ConsoleKit,它通过 DBus 提供一些会话信息,包括 “空闲提示”;这适用于 X11 和文本登录。

(但是,有计划弃用 ConsoleKit,将其部分内容放入 systemd 中;“空闲提示”功能的未来尚未确定。)


为了完整起见, os.stat(ttydev).st_mtimeos.fstat(1).st_mtime 返回 tty/pty 设备的上次输入时间。

Most Linux distributions come with ConsoleKit, which provides some session information over DBus, including an "idle hint"; this works for both X11 and text logins.

(However, there are plans to deprecate ConsoleKit, moving parts of it into systemd; the future for the "idle hint" feature hasn't been decided yet.)


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

薄荷梦 2024-11-13 00:55:13

在你的计算机上安装一个网络摄像头,每五秒抓取一张图像,然后有一些用于图像分析的 python 模块可以检查你是否仍然坐在座位上。

或者将一个微型开关连接到您的椅子上,将其连接到您的 PC 串行端口(或现代 USB 端口之一)并从 Python 读取该数据...

Stick a webcam on your computer that grabs an image every five seconds, then there's some python modules for image analysis that can check if you are still sitting in your seat.

Or get a microswitch wired into your chair, connect that to your PC serial port (or one of those modern USB ports) and read that from Python...

靑春怀旧 2024-11-13 00:55:13

导入这个脚本。当其他应用程序(例如 Firefox)开始抢占带宽时,请暂停主程序。涉及一些简单的添加,但除此之外它非常简单

import this script. When another app like firefox starts grabbing bandwidth, pause the main program. There's some simple addition involed but other than that it's pretty easy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文