How can I prevent Windows 10 from any kind of sleep or hibernate when running python script?
Solution 1
Use the tool Don't Sleep while running your python script:
Don't Sleep is a small portable program to prevent system shutdown, Standby, Hibernate, Turn Off and Restart.
This prevents all action that can interrupt your script.
Solution 2
Runs a program preventing sleeping or the display turning off while the program runs. Doesn't affect the screensaver.
Using pywin32
library you can do exactly the same.
I note that pywin32
doesn't actually wrap this function.
For more info see docs https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
NOTE: Do not use the constant
ES_USER_PRESENT
as it will always fail.
From above link
ES_AWAYMODE_REQUIRED 0x00000040 Enables away mode. This value must be specified with ES_CONTINUOUS. Away mode should be used only by media-recording and media-distribution applications that must perform critical background processing on desktop computers while the computer appears to be sleeping. See Remarks.
ES_CONTINUOUS 0x80000000 Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.
ES_DISPLAY_REQUIRED 0x00000002 Forces the display to be on by resetting the display idle timer.
ES_SYSTEM_REQUIRED 0x00000001 Forces the system to be in the working state by resetting the system idle timer.
To Use
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
Posted to my repository as well https://winsourcecode.blogspot.com/2020/05/keepdisplayon-runs-program-preventing.html
Related videos on Youtube
Zepeng Huo
Updated on September 18, 2022Comments
-
Zepeng Huo over 1 year
I'm running a python script, which takes a couple of days. But Windows 10 seems keep pausing the script after some time, i.e. when I leave for a while the program stops (screen off also), but when I come back and do whatever things, the program will resume. Although I change all the power options in Windows 10, like
"never go to sleep when plugged in"
"never turn off hard disk"
"allow hybrid sleep: off"
"never hibernate"
Extra information: Windows edition: Windows 10 pro; 64-bit Operating System, x64-based processor;
python: Python 2.7.12 |Continuum Analytics, Inc.| (default, Jun 29 2016, 11:42:13) [MSC v.1500 32 bit (Intel)] on win32
-
Admin about 7 yearsIn the power plan, I also chose the
High performance
-
Admin about 7 yearsSettings->power and sleep->[set the screen and sleep times to 'never']
-
Admin about 7 years@ssaltman, that's the first example I listed above, "never go to sleep when plugged in"
-