【问题标题】:Python/Windows 10 - Is there a way to prevent a Windows 10 computer from going to sleep in Python?Python/Windows 10 - 有没有办法防止 Windows 10 计算机在 Python 中进入睡眠状态?
【发布时间】:2020-09-02 04:15:44
【问题描述】:

有没有办法防止 Windows 10 计算机在 Python 中进入睡眠状态?

与此处发布的问题基本相同,但适用于 Windows 10:Prevent OS X from going to sleep with Python?

有没有可以从 Python 调用的 Windows 命令?

如果这有什么不同的话,我正在使用 Python 3.6。

【问题讨论】:

标签: python python-3.x windows-10 python-3.6 sleep


【解决方案1】:

这可能不是一个直接的答案 & 不是一种优雅的方式,但会起作用。您可以使用 PyAutoGUI 以特定间隔单击特定屏幕位置。这将迫使您的计算机不休眠

import time
import pyautogui

# press ctrl+c to get out of the loop
while(True):
    pyautogui.click(x=100, y=200) # make sure nothing is there in the clicked location
    time.sleep(10)

【讨论】:

    【解决方案2】:

    对于以后看到这个的人,我强烈推荐 kbarnes3 的这个装饰器:https://gist.github.com/kbarnes3/3fb7d353e9bdd3efccd5 只需下载文件,将其放入您的脚本目录并使用如下:

    from powermanagement import long_running
    
    @long_running
    def your_function():
        #do stuff
    
    your_function()
    

    只要 your_function() 正在运行,电脑就不会关闭屏幕,因此不会进入睡眠状态。

    它通过使用 ctypes 模块和线程执行状态来工作。据我所知,这也是大多数媒体播放器等使用的方法。这是一个非常干净的解决方案,因为您不必移动鼠标或按键。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 2017-04-10
      • 2015-06-06
      • 2012-07-14
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      相关资源
      最近更新 更多