![运行 Python 脚本时,如何防止 Windows 10 进入任何类型的睡眠或休眠状态?](https://linux22.com/image/1528493/%E8%BF%90%E8%A1%8C%20Python%20%E8%84%9A%E6%9C%AC%E6%97%B6%EF%BC%8C%E5%A6%82%E4%BD%95%E9%98%B2%E6%AD%A2%20Windows%2010%20%E8%BF%9B%E5%85%A5%E4%BB%BB%E4%BD%95%E7%B1%BB%E5%9E%8B%E7%9A%84%E7%9D%A1%E7%9C%A0%E6%88%96%E4%BC%91%E7%9C%A0%E7%8A%B6%E6%80%81%EF%BC%9F.png)
我正在运行一个 Python 脚本,这需要几天时间。但 Windows 10 似乎会在一段时间后暂停脚本,即当我离开一段时间时程序会停止(屏幕也会关闭),但当我回来做任何事情时,程序都会恢复。虽然我更改了 Windows 10 中的所有电源选项,例如
“插入电源后永不休眠”
“永不关闭硬盘”
“允许混合睡眠:关闭”
“永不冬眠”
额外信息:Windows 版本:Windows 10 pro;64 位操作系统,基于 x64 的处理器;
python:Python 2.7.12 |Continuum Analytics, Inc.|(默认,2016 年 6 月 29 日,11:42:13)[MSC v.1500 32 位(Intel)] 在 win32 上
答案1
答案2
运行一个程序以防止程序运行时进入睡眠状态或关闭显示器。不会影响屏幕保护程序。
使用pywin32
库可以做同样的事情。我注意到pywin32
实际上并没有包装这个函数。
更多信息请参阅文档https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
注意:不要使用常量,
ES_USER_PRESENT
因为它总是会失败。
来自上面的链接
ES_AWAYMODE_REQUIRED 0x00000040 启用离开模式。此值必须与 ES_CONTINUOUS 一起指定。离开模式应仅由媒体录制和媒体分发应用程序使用,这些应用程序必须在计算机处于睡眠状态时在桌面计算机上执行关键的后台处理。请参阅备注。
ES_CONTINUOUS 0x80000000 通知系统,设置的状态应保持有效,直到下一次使用 ES_CONTINUOUS 和其他状态标志之一的调用被清除。
ES_DISPLAY_REQUIRED 0x00000002 通过重置显示空闲计时器强制打开显示器。
ES_SYSTEM_REQUIRED 0x00000001 通过重置系统空闲计时器强制系统处于工作状态。
使用
KeepDisplayOn <commandline of program to run>
KeepSystemOn <commandline of program to run>
@Echo Off
ECHO Three files follow
ECHO PreventSleep.bat
ECHO.
ECHO This file compiles KeepDisplayOn.vb and KeepSystemOn.vb to KeepDisplayOn.exe and KeepSystemOn.exe using the system VB.NET compiler.
ECHO.
ECHO Runs a program preventing sleeping or the display turning off while the program runs
ECHO.
ECHO To Use
ECHO KeepDisplayOn ^<commandline of program to run^>
ECHO KeepSystemOn ^<commandline of program to run^>
ECHO.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\KeepDisplayOn.vb" /out:"%~dp0\KeepDisplayOn.exe" /target:winexe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\KeepSystemOn.vb" /out:"%~dp0\KeepSystemOn.exe" /target:winexe
pause
'KeepSystemOn.vb
imports System.Runtime.InteropServices
Public Module MyApplication
Public Declare UNICODE Function SetThreadExecutionState Lib "Kernel32" (ByVal esFlags as Integer) as Integer
Public Const ES_AWAYMODE_REQUIRED = &h40
Public Const ES_CONTINUOUS = &h80000000
Public Const ES_DISPLAY_REQUIRED = &h2
Public Const ES_SYSTEM_REQUIRED = &h1
Public Const ES_USER_PRESENT = &h4
Public Sub Main ()
Dim wshshell as Object
Dim Ret as Integer
WshShell = CreateObject("WScript.Shell")
Ret = SetThreadExecutionState(ES_Continuous + ES_System_Required + ES_Awaymode_Required)
WshShell.Run(Command(), , True)
End Sub
End Module
'KeepDisplayOn.vb
imports System.Runtime.InteropServices
Public Module MyApplication
Public Declare UNICODE Function SetThreadExecutionState Lib "Kernel32" (ByVal esFlags as Integer) as Integer
Public Const ES_AWAYMODE_REQUIRED = &h40
Public Const ES_CONTINUOUS = &h80000000
Public Const ES_DISPLAY_REQUIRED = &h2
Public Const ES_SYSTEM_REQUIRED = &h1
Public Const ES_USER_PRESENT = &h4
Public Sub Main ()
Dim wshshell as Object
Dim Ret as Integer
WshShell = CreateObject("WScript.Shell")
Ret = SetThreadExecutionState(ES_Continuous + ES_Display_Required + ES_Awaymode_Required)
WshShell.Run(Command(), , True)
End Sub
End Module
也发布到我的存储库https://winsourcecode.blogspot.com/2020/05/keepdisplayon-runs-program-preventing.html