how do i sleep my computer from within python?

  • Last Update :
  • Techknowledgy :

You can use

import os
os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")

If this command doesn't work for you (it actually puts your comp. in hibernation mode) you will need to turn hibernation off:

Powercfg - H OFF

To turn hibernation mode back on:

Powercfg - H ON

If you don't want to turn hibernation off just to run a command, you can use psshutdown as an alternative

C: \Windows\ System32\ psshutdown.exe - d - t 0
rundll32.exe powrprof.dll, SetSuspendState 0, 1, 0

Suggestion : 2

The sleep() function suspends (waits) execution of the current thread for a given number of seconds. ,The sleep() function suspends execution of the current thread for a given number of seconds.,The sleep() function suspends execution of the current thread for a given number of seconds.,In case of single-threaded programs, sleep() suspends execution of the thread and process. However, the function suspends a thread rather than the whole process in multithreaded programs.

Example 1: Python sleep()

import time

print("Printed immediately.")
time.sleep(2.4)
print("Printed after 2.4 seconds.")

Example 2: Python create a digital clock

import time

while True:
   localtime = time.localtime()
result = time.strftime("%I:%M:%S %p", localtime)
print(result)
time.sleep(1)

When you run the program, the output will be something like:

02: 10: 50 PM
02: 10: 51 PM
02: 10: 52 PM
02: 10: 53 PM
02: 10: 54 PM
   ........

All the programs above in this article are single-threaded programs. Here's an example of a multithreaded Python program.

import threading

def print_hello_three_times():
   for i in range(3):
   print("Hello")

def print_hi_three_times():
   for i in range(3):
   print("Hi")

t1 = threading.Thread(target = print_hello_three_times)
t2 = threading.Thread(target = print_hi_three_times)

t1.start()
t2.start()

Example 4: sleep() in a multithreaded program

import threading
import time

def print_hello():
   for i in range(4):
   time.sleep(0.5)
print("Hello")

def print_hi():
   for i in range(4):
   time.sleep(0.7)
print("Hi")

t1 = threading.Thread(target = print_hello)
t2 = threading.Thread(target = print_hi)
t1.start()
t2.start()

Suggestion : 3

The method sleep() suspends execution for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time.,The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal's catching routine.,t − This is the number of seconds execution to be suspended.,We make use of First and third party cookies to improve our user experience. By using this website, you agree with our Cookies Policy. Agree Learn more

Following is the syntax for sleep() method −

time.sleep(t)
2._
#!/usr/bin/python3

import time

print("Start : %s" % time.ctime())
time.sleep(5)
print("End : %s" % time.ctime())

When we run the above program, it produces the following result −

Start: Mon Feb 15 12: 08: 42 2016
End: Mon Feb 15 12: 08: 47 2016

Suggestion : 4

Simple cross-platform wakelock written in Python. Prevent your computer from going to sleep in the middle of a long running task.,Starts the program. While running, computer will not go to sleep. If battery is running out, your OS might force laptop to sleep.,wakepy is simple and it has a little amount of code. You can read the whole source code quickly,The program launches a caffeinate in a subprocess when setting keepawake, and terminates the subprocess when unsetting. This does not prevent the user from manually sleeping the system or terminating the caffeinate process.

Installing

pip install wakepy

Start from command line

python - m wakepy

CLI

python - m wakepy[-h][-s]

optional arguments:
   -h, --help show this help message and exit -
   s, --keep - screen - awake Keep also the screen awake.On Linux, this flag is set on and cannot be disabled.

new in version 0.4.0

from wakepy
import keepawake

with keepawake(keep_screen_awake = False):
   ...# do stuff that takes long time