2

I have a Python script doing some job which takes up to 5 minutes, then it sleeps for an hour and starts again. Now I want my laptop to sleep instead of being always on while waiting, and to wake up roughly every hour just to run the job. Is it possible to sleep and wake up with Python?

I am using Windows 7.

nikola
  • 2,241
  • 4
  • 30
  • 42
anton
  • 21
  • 1
  • 1
  • 2

3 Answers3

5

It looks like this question addresses a solution:

import os
os.system(r'%windir%\system32\rundll32.exe powrprof.dll,SetSuspendState Hibernate')

which is actually just using the native command line utility for sleeping.

This post from MSFT will explain how you can wake the computer.

Community
  • 1
  • 1
rofls
  • 4,993
  • 3
  • 27
  • 37
1

You should better create a Task Scheduler task in Windows instead, that runs your python script on schedule and wakes up the PC if needed (the task's setting). To put it to sleep just set up energy settings to sleep after several minutes of inactivity.

mderk
  • 796
  • 4
  • 13
  • 1
    what should trigger awake? i don't know the time because the job can take a while. Isnt inactivity assumes no keyboard/mouse actions? If so, then computer will be considered inactive while running the script and may be sent to sleep – anton Feb 16 '13 at 21:44
  • The windows scheduled task can be configured to wake the computer up, so this is not a problem. for more info: http://www.7tutorials.com/advanced-users-task-creation-task-scheduler http://www.howtogeek.com/119028/how-to-make-your-pc-wake-from-sleep-automatically/ – mderk Feb 16 '13 at 22:07
  • You will have to configure the BIOS of the computer properly so that the OS is allowed to wake the computer. In many BIOS versions the setting is called "Wake controlled by" -> "OS". – nikola Feb 17 '13 at 14:17
0

You could try to make a Visual Basic Script (.vbs) or Batch (.bat) program and run it from python. The program should have code to run the sleep/wake up function.

Then again, since you are using windows, just use task scheduler.

Trooper Z
  • 1,617
  • 14
  • 31