Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent sleep mode python (Wakelock on python)

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)

like image 450
Max Dev Avatar asked Aug 25 '19 14:08

Max Dev


People also ask

How do I stop Windows from sleeping in Python?

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.

Will Python script run when computer sleeps?

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.

How do I keep my computer from going to sleep?

Go to Control Panel > Personalization > Change Screensaver. Next to On Resume, Display Logon Screen, uncheck the box. This prevents your system from sleeping.


Video Answer


1 Answers

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.

CLI

python -m wakepy [-s]

Using the optional -s flag will also keep the screen on.

Python API

from wakepy import set_keepawake, unset_keepawake

set_keepawake(keep_screen_awake=False)
# do stuff that takes long time
unset_keepawake()
like image 75
np8 Avatar answered Oct 18 '22 19:10

np8