anton
anton

Reputation: 21

Sleep and wake with python

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.

Upvotes: 2

Views: 10901

Answers (3)

Trooper Z
Trooper Z

Reputation: 1661

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.

Upvotes: 0

rofls
rofls

Reputation: 5115

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.

Upvotes: 5

mderk
mderk

Reputation: 794

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.

Upvotes: 1

Related Questions