Keep your system on and screen on, it will not fall asleep. It is useful in some long term calculations.
pip install keep_awakeThis module exposes only two methods: prevent_sleep and allow_sleep, both of them taks no arguments.
prevent_sleep returns a boolean value that indicates whether the operation is successful. After you call this method, your system and screen will keep working until you invoke allow_sleep.
You won't have any exceptions by calling any methods twice, or calling allow_sleep without calling prevent_sleep, they have built-in protection.
If you forget to call allow_sleep before exiting your program, it doesn't matter, it will back to usual once the module is released.
We also provide a context manager for you to manage the state automatically, just like this:
from keep_awake import KeepAwakeGuard
with KeepAwakeGuard():
# do your work hereBoth arm64 and x64 are OK, this code only depends on IOKit and CoreFoundation, which is built-in macOS Framework.
Methods on macOS are concurrent safe.
We didn't test widely on various versions of Windows because I have no more Windows computer :(
But basically, it can work fine on Windows 10 x64 and Windows 11 x64. I have already tested.
Methods on Windows are concurrent safe.
Linux distros are various, we can only support Gnome and KDE. They have dbus api so this part is implemented in pure python. If you need this feature, please install with this command:
pip install keep_awake[linux]If you failed to install, try to use your package manager to install cmake, pkg-config, libdbus-1-dev and libglib2.0-dev.
Methods on Linux are concurrent safe.
We use uv to manage the project, so it is very easy to build and test it.
For building:
uv buildFor testing
uv run pytest