0

Is there a way to prevent a Windows 10 computer from going to sleep in Python?

Basically same question as posted here but for Windows 10: Prevent OS X from going to sleep with Python?

Is there a Windows command I could call from Python?

I'm using Python 3.6 if that makes any difference.

Emet
  • 3
  • 3
  • maybe `powercfg -x -standby-timeout-ac 0` will do it. – moe asal May 16 '20 at 09:18
  • 3
    Does this answer your question? [Prevent sleep mode python (Wakelock on python)](https://stackoverflow.com/questions/57647034/prevent-sleep-mode-python-wakelock-on-python) – Qiu May 16 '20 at 09:23

2 Answers2

4

For anyone seeing this in the future, i highly recommend this decorator from kbarnes3: https://gist.github.com/kbarnes3/3fb7d353e9bdd3efccd5 Just download the file, put it in your script directory and use as follows:

from powermanagement import long_running

@long_running
def your_function():
    #do stuff

your_function()

As long as your_function() is running, the pc won't turn the screen off and thus not go to sleep.

It works by using the ctypes module and thread Execuion States. As far as I know this is also the method used by most media players etc. And it's a really clean solution since you don't have to move your mouse or press keys.

Domi
  • 123
  • 8
0

It may be not a direct answer & not an elegant way but will work. You can use PyAutoGUI to click on a specific screen location at a specific interval. That will force your computer not to sleep

import time
import pyautogui

# press ctrl+c to get out of the loop
while(True):
    pyautogui.click(x=100, y=200) # make sure nothing is there in the clicked location
    time.sleep(10)
Epsi95
  • 8,832
  • 1
  • 16
  • 34