Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Linux from sleeping when Python process running

Is there a (programmatic) way to prevent my Linux (Ubuntu) from going into sleep mode while my Python program is running? I seek a solution that avoids me manually changing the OS power settings each time I run my code.

Background: I'm running a long machine learning training from within PyCharm (using Ray RLlib). While I would have hoped for the intense active process to prevent the sleeping, my Ubuntu falls asleep after my standard power settings' suspension period is up if I don't interact during the training.

I am surprised to not find any solution when googling and looking at Stackoverflow; it would seem such a common need for Python (or maybe other) programmers, and I would have expected some python tools to exist to readily allow to prevent the sleeping from within my Python script itself.

I found SetThreadExecutionState for Windows (with C++), but nothing so far for Linux. A slightly less focused question Prevent sleep mode python (Wakelock on python) is also related to my issue, but it has not attracted any answer solving the case for the Linux OS.

like image 539
FlorianH Avatar asked Oct 14 '25 08:10

FlorianH


1 Answers

You can try Deluge-PreventSuspendPlus, version of Deluge-PreventSuspend-plugin. It is in python, uses DBus and Gnome Session Manager, but you can adapt it as well.

There are "inhibitor" classes in core.py where you will find how exactly it works.

like image 59
tansy Avatar answered Oct 16 '25 21:10

tansy