在 Unix 上的 Python 中,确定我是否正在使用我的计算机?还是闲着?
我想编写一个脚本来在后台进行大量网络上传。 但是,我希望它在我使用计算机时暂停(通过检测网络活动或键盘活动或者我不闲着)。
检测我正在使用 Unix 上的 Python 计算机的最佳方法是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 X11/XScreenSaver 获取空闲时间的 Unixy 解决方案:(
来自“Python 中的 X11 空闲时间和聚焦窗口”,最初位于 thp.io,现在显然只有 GitHub要点仍然存在。)
清理部分在后来的编辑中添加到代码中由另一个用户 以便可以定期调用它。
正如他们引用的答案的评论中所指出的,请注意,您还应该对函数调用进行正确的返回代码检查,以避免当 X 显示和其他初始化由于某种原因失败时程序异常终止。
Unixy solution using X11/XScreenSaver to get idle time:
(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.
我猜您担心文件传输的网络活动会妨碍交互式用户。您无需担心用户是否正在键盘上打字。真正重要的是是否存在竞争性网络活动。
例如,在 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.
大多数 Linux 发行版都附带 ConsoleKit,它通过 DBus 提供一些会话信息,包括 “空闲提示”;这适用于 X11 和文本登录。
(但是,有计划弃用 ConsoleKit,将其部分内容放入 systemd 中;“空闲提示”功能的未来尚未确定。)
为了完整起见,
os.stat(ttydev).st_mtime
或os.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
oros.fstat(1).st_mtime
returns last input time for tty/pty devices.在你的计算机上安装一个网络摄像头,每五秒抓取一张图像,然后有一些用于图像分析的 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...
导入这个脚本。当其他应用程序(例如 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