Skip to content

Instantly share code, notes, and snippets.

@jonasbits
Forked from kbarnes3/powermanagement.py
Created October 2, 2016 16:20
Show Gist options
  • Save jonasbits/c88bb3224ba3dea84a722abc33eea9d7 to your computer and use it in GitHub Desktop.
Save jonasbits/c88bb3224ba3dea84a722abc33eea9d7 to your computer and use it in GitHub Desktop.
# This file provides a long_running decorator to indicate that a function needs a long amount of time to complete and
# the computer should not enter standby. This file currently only works on Windows and is a no-op on other platforms.
import ctypes
import platform
ES_CONTINUOUS = 0x80000000
ES_SYSTEM_REQUIRED = 0x00000001
def _set_thread_execution(state):
ctypes.windll.kernel32.SetThreadExecutionState(state)
def prevent_standby():
if platform.system() == 'Windows':
_set_thread_execution(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)
def allow_standby():
if platform.system() == 'Windows':
_set_thread_execution(ES_CONTINUOUS)
def long_running(func):
def inner(*args, **kwargs):
prevent_standby()
result = func(*args, **kwargs)
allow_standby()
return result
return inner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment