Cross-platform wakelock / keep-awake / stay-awake written in Python.
Wakepy is a package with an Python API and a CLI tool for keeping a system awake. It has two main modes:
⌛ Keeping CPU awake: For long running tasks. Inhibit the automatic, timer based sleep or suspend action, but allow screenlock and screensaver turning on and monitor turning off. E.g. for training machine learning models, video encoding and web scraping. (See: keep.running
)
🖥️ Keeping screen awake: For long running tasks which require also the screen on and screenlock and screensaver inhibited. E.g. for showing a video and dashboard / monitoring apps. (See: keep.presenting
)
Wakepy may keep the following systems awake. ⌛: keep.running mode, 🖥️:keep.presenting mode.
Runtime environment | Methods | Modes |
---|---|---|
Windows[1] | SetThreadExecutionState | ⌛ 🖥️ |
macOS[2] | caffeinate | ⌛ 🖥️ |
Unix + GNOME[3] | org.gnome.SessionManager org.freedesktop.ScreenSaver |
⌛ 🖥️ |
Unix + KDE Plasma[4] | org.freedesktop.PowerManagement org.freedesktop.ScreenSaver |
⌛ 🖥️ |
Unix + Freedesktop.org DE[5] |
org.freedesktop.PowerManagement org.freedesktop.ScreenSaver |
⌛ 🖥️ |
Unix above refers to any Unix-like systems which might use such DEs, e.g. Linux or FreeBSD. See also: Wakepy roadmap.
Wakepy supports CPython 3.7 to 3.13 and PyPy 3.8 to 3.10, and may be installed from PyPI with
pip install wakepy
Wakepy can also be installed from conda-forge with
conda install wakepy
For more details and install options, see: Installing documentation.
[!NOTE] To get the
wakepy
CLI command working, you might need to restart the shell / terminal application.
Here's some reasons why you might want to consider using wakepy:
keep.running
mode you can disable just the automatic suspend and keep the automatic screen lock untouched.To keep system from sleeping, run
wakepy
For presentation mode, add -p
flag. See also: CLI API.
In the simplest case, keeping a system running long running task with wakepy would be in python (See: keep.running):
from wakepy import keep
with keep.running():
# Do something that takes a long time. The system may start screensaver
# / screenlock or blank the screen, but CPU will keep running.
If you want to also prevent screen lock and screen blank, use the keep.presenting mode:
from wakepy import keep
with keep.presenting():
# Do something that takes a long time and requires the screen to be awake
[!TIP] See the User Guide and the available wakepy Modes and Methods
[!NOTE] Wakepy API is still experimental 🚧
Since wakepy is still 0.x.x, the API might change without further notice from one release to another. After that, breaking changes should occur only part of a major release (e.g. 1.x.x -> 2.0.0).
Wakepy vision is to support any† environment which runs Python. The following runtime environments will get support in the future†. Please vote or comment on the issue to raise them towards top of priorities. I'm also happy to receive PRs or comments explaining how it could be implemented.
†: if technically possible.
Runtime environment | Issue |
---|---|
WSL | wakepy/#36 |
cygwin | wakepy/#363 |
Unix + Cinnamon | wakepy/#312 |
Unix + Xfce | wakepy/#311 |
Unix + Mate | wakepy/#314 |
Unix + LXQt | wakepy/#313 |
Unix + systemd | wakepy/#335 |
ChromeOS | wakepy/#364 |
Android | wakepy/#358 |
Jupyter Notebook (hosted on eg. Google Colab) | wakepy/#195 |
Browser (Pyodide, PyPy.js, Brython, Transcrypt, Skulpt) | wakepy/#362 |
If you have ideas or comments, please post yours on wakepy/#317.
The contents of this repository are licensed with MIT License, which is permissive and allows you to use the code as part of any application or library, commercial or not, with the following exception: The GitHub Invertocat logo used in the social share image is property of GitHub, downloaded from github.com/logos and is used under the terms specified by GitHub.
[1] Windows XP or higher. Windows Server 2003 or higher. |
[2] Mac OS X 10.8 Mountain Lion (July 2012) or newer. |
[3] GNOME 2.24 (Sept 2008) onwards. |
[4] KDE Plasma 5.12.90 (May 2018) onwards. |
[5] Freedesktop.org compliant Desktop Environments on Unix-line (Linux/BSD) system which implements the listed D-Bus interfaces. |