I've written a file sync and verify program in Python. When the volume of files is large, there's a risk that my Windows machine will go into sleep mode before the program finishes. In order to prevent this, I include a call to PowerManagement.prevent_sleep() before the lengthy processing, and call PowerManagement.allow_sleep() afterwards (see source code, below).
In the past, I've written code to accomplish this by switching between Windows power plans.

Unfortunately, there are some significant drawbacks to doing so:
The following code avoids these drawbacks by setting the program's thread execution state to values that will allow or prevent the OS from going into sleep mode:
import ctypes
import platform
from Globals import app_globals
from StringLiterals import StringLiterals
class PowerManagement:
# https://trialstravails.blogspot.com/2017/03/preventing-windows-os-from-sleeping.html
_ES_CONTINUOUS = 0x80000000
_ES_SYSTEM_REQUIRED = 0x00000001
@staticmethod
def prevent_sleep():
if platform.system() == StringLiterals.PLATFORM_WINDOWS:
app_globals.log.print("Preventing Windows from going to sleep")
ctypes.windll.kernel32.SetThreadExecutionState(
PowerManagement._ES_CONTINUOUS | PowerManagement._ES_SYSTEM_REQUIRED)
@staticmethod
def allow_sleep():
if platform.system() == StringLiterals.PLATFORM_WINDOWS:
app_globals.log.print("Allowing Windows to go to sleep")
ctypes.windll.kernel32.SetThreadExecutionState(PowerManagement._ES_CONTINUOUS)
The above source code is based on ideas from another developer. See the link in the comment.
Sometimes it's just impossible to stay awake.
Complete source code available here.
| Title | Date |
| Node.js + Express: How to Block Requests by User-Agent Headers | January 7, 2026 |
| Vault 3 is Now Available for Windows on ARM Machines! | December 13, 2025 |
| Vault 3: How to Include Outline Text in Exported Photos | October 26, 2025 |
| .NET Public-Key (Asymmetric) Cryptography Demo | July 20, 2025 |
| Raspberry Pi 3B+ Photo Frame | June 17, 2025 |
| EBTCalc (Android) Version 1.53 is now available | May 19, 2024 |
| Vault 3 Security Enhancements | October 24, 2023 |