How I can prevent sleep-mode on python without using extra apps on different OS(Ubuntu, Windows...) but in most cases I need Linux solution
I am making app that works big amount of time. It uses like 80% of CPU, so a user just start this app and go away from keyboard. So I think I need something like system api or library that lock sleep-mode. I am sure that, it exists. For example, if you open any video player on your OS, your (PC, Laptop) will not go to sleep mode, the same thing in browser.
Also, there is the same thing in Android (WakeLock) or Windows (SetThreadExecutionState)
Using Python's PyAutoGUI and PySimpleGUI To make it easier to use and of course shareable, I built a UI on top of the underlying script that keeps windows active. The UI is extremely simple, and you can simply close it when you'd like the script to stop running.
You don't need to worry about the screen locking or turning off as these wont affect running processes in python. However, if your system is set to sleep after a set amount of time you may need to change this to Never. Keep in mind there are separate settings depending on whether or not the system is plugged in.
Go to Control Panel > Personalization > Change Screensaver. Next to On Resume, Display Logon Screen, uncheck the box. This prevents your system from sleeping.
While googling around to find a solution there was no package available, so I decided to package it to put it to PyPI: wakepy. I got PRs for cross-platform support, and currently wakepy supports Windows, Linux and macOS.
python -m wakepy [-s]
Using the optional -s
flag will also keep the screen on.
from wakepy import set_keepawake, unset_keepawake
set_keepawake(keep_screen_awake=False)
# do stuff that takes long time
unset_keepawake()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With