简体   繁体   English

防止 Linux 在 Python 进程运行时休眠

[英]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?在我的 Python 程序运行时,是否有一种(编程的)方法来防止我的 Linux(Ubuntu)进入睡眠模式? 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).背景:我正在 PyCharm 中进行长时间的机器学习培训(使用 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.虽然我希望通过激烈的活动过程来防止睡眠,但如果我在训练期间不进行交互,我的 Ubuntu 在标准电源设置的暂停期结束后就会睡着。

I am surprised to not find any solution when googling and looking at Stackoverflow;在谷歌搜索和查看 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.对于 Python(或其他)程序员来说,这似乎是一种普遍的需求,我本以为会存在一些 Python 工具,以便很容易地防止我的 Python 脚本本身进入休眠状态。

I found SetThreadExecutionState for Windows (with C++), but nothing so far for Linux.我找到了适用于 Windows(使用 C++)的SetThreadExecutionState ,但目前还没有找到适用于 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.一个不太集中的问题Prevent sleep mode python (Wakelock on python)也与我的问题有关,但它没有吸引任何解决 Linux 操作系统案例的答案。

You can try Deluge-PreventSuspendPlus , version of Deluge-PreventSuspend-plugin .您可以尝试Deluge-PreventSuspendPlusDeluge-PreventSuspend-plugin 的版本 It is in python, uses DBus and Gnome Session Manager, but you can adapt it as well.它使用 Python 编写,使用 DBus 和 Gnome 会话管理器,但您也可以对其进行调整。

There are "inhibitor" classes in core.py where you will find how exactly it works. core.py中有“抑制剂”类,您可以在其中找到它的工作原理。

sudo systemctl mask sleep.target sudo systemctl 掩码 sleep.target

sudo systemctl mask suspend.target sudo systemctl 掩码 suspend.target

sudo systemctl mask hibernate.target sudo systemctl 掩码 hibernate.target

sudo systemctl mask hybrid-sleep.target sudo systemctl 掩码 hybrid-sleep.target

see the web page: https://linux-tips.us/how-to-disable-sleep-and-hibernation-on-ubuntu-server/查看网页: https ://linux-tips.us/how-to-disable-sleep-and-hibernation-on-ubuntu-server/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM