Reputation: 13
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.
Upvotes: 1
Views: 2165
Reputation: 143
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.
Upvotes: 5
Reputation: 9047
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)
Upvotes: 1