Prevent sleep mode python (Wakelock on python)

我与影子孤独终老i 提交于 2020-05-12 04:40:50

问题


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)


回答1:


I ran into similar situation where a process took long enough to execute itself that windows would hibernate. To overcome this problem I wrote a script.

The following simple piece of code can prevent this problem. When used, it will ask windows not to sleep while the script runs. (In some cases, such as when the battery is running out, Windows will ignore your request.)

    class WindowsInhibitor:
        '''Prevent OS sleep/hibernate in windows; code from:
        https://github.com/h3llrais3r/Deluge-PreventSuspendPlus/blob/master/preventsuspendplus/core.py
        API documentation:
        https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx'''
        ES_CONTINUOUS = 0x80000000
        ES_SYSTEM_REQUIRED = 0x00000001

        def __init__(self):
            pass

        def inhibit(self):
            import ctypes
            print("Preventing Windows from going to sleep")
            ctypes.windll.kernel32.SetThreadExecutionState(
                WindowsInhibitor.ES_CONTINUOUS | \
                WindowsInhibitor.ES_SYSTEM_REQUIRED)

        def uninhibit(self):
            import ctypes
            print("Allowing Windows to go to sleep")
            ctypes.windll.kernel32.SetThreadExecutionState(
                WindowsInhibitor.ES_CONTINUOUS)

To run the script, simply :

    import os

    osSleep = None
    # in Windows, prevent the OS from sleeping while we run
    if os.name == 'nt':
        osSleep = WindowsInhibitor()
        osSleep.inhibit()

    # do slow stuff

    if osSleep:
        osSleep.uninhibit()



回答2:


The logic in Keep.Awake will solve your problem on ubuntu or any Linux Distro running Gnome (old and new including Unity), works on both Wayland and X. It is easy to use.

I posted a solution to a similar question here: https://askubuntu.com/a/1231975/183131

So logic-wise do the following:

  1. Use DBus command via subprocess.Popen(...) to clear the sleep/suspend counter.
  2. Use psutil.cpu_percent() to query the the amount of cpu usage periodically or just put logic once your program is done doing its thing to re-set the sleep config.

You can view the code here for details or hints on how you can mod your code: https://launchpad.net/keep.awake

Alternatively you can just run keepawake.py on the Linux box you're running your cpu-intensive program on and it will solve your problem! It just works!

Example usage taken from the webpage:

To run as background service and set minimum CPU load as 13%:

nohup ./keepawake.py -c 13 -r > /dev/null 2>&1 &

To run as background service and set 15 min (900 sec) as the user activity idle time before it determines that the user is idle:

nohup ./keepawake.py -u 900 -r > /dev/null 2>&1 &

To run as background service and set minimum network traffic as 5KB (5120 bytes):

nohup ./keepawake.py -s 5120 -r > /dev/null 2>&1 &

To run as background service and set the schedule to sleep/suspend after 1 hour (this value is only set if user-activity, cpu, and network traffic are all determined to be idle) :

nohup ./keepawake.py -w 3600 -r > /dev/null 2>&1 &

To run all settings above (network, CPU, User idle, sleep schedule) in the one go and set the log-file path to "/home/$USER/sleep/log/Keep.Awake/" with detailed output:

nohup ./keepawake.py -s 5120 -c 13 -u 900 -w 3600 -l /home/$USER/sleep/log/Keep.Awake/ -v Detail -r > /dev/null 2>&1 &


来源:https://stackoverflow.com/questions/57647034/prevent-sleep-mode-python-wakelock-on-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!