Python time.sleep() Method

How can I make a time delay in Python?

Python sleep() method used to suspend the execution for given of time(in seconds). We can use python sleep function to halt the execution of the program for given time in seconds. 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. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system. You can set a delay in your Python script by passing the number of seconds you want to delay to the sleep function.

import time time.sleep(5) #delay for 5 seconds

When you run the above example, it will end only after five seconds.

The sleep() method supports floating point numbers, meaning you can make it wait fractions of a second too.

import time time.sleep(1.500)

When you run the above example, the program wait for 1 second and 500 milliseconds to finish.

  1. time.sleep(1) # sleep for 1 seconds
  2. time.sleep(60) # sleep for 1 minute
  3. time.sleep(3600) # sleep for 1 hour

Time delay for infinite loop

Here is another example where something is run approximately each 5 seconds.

import time while True: print("Thi smessage prints each 5 seconds") time.sleep(5) # Delay for 5 seconds

The above program run an infinite loop, so you should forcefully stop the program when you want.

Count down program with sleep

The following program is a countdown example, using sleep method to wait 1 second each number.

import time wait = 10 while wait > 0: print(wait) time.sleep(1) wait = wait - 1

Thread and Sleep

Threads are usually contained in processes. More than one thread can exist within the same process. These threads share the memory and the state of the process. From the following example you can see how sleep() method work in a multi treaded program.

import time from threading import Thread class Main_Thread(Thread): def run(self): for x in range(100, 104): print(x, " - Main Thread") time.sleep(5) class Service_Thread(Thread): def run(self): for x in range(1, 16): print(x) time.sleep(1) Main_Thread().start() Service_Thread().start()
100 - Main Thread 1 2 3 4 5 101 - Main Thread 6 7 8 9 10 102 - Main Thread 11 12 13 14 15 103 - Main Thread

Accuracy of time.sleep()

The time.sleep(seconds) is not real time. The time.sleep() function uses the underlying operating system's sleep() function, sometimes it may be off in terms of milliseconds. Most PC machines have hardware limits in the 1-10ms range, regardless of operating system. To the operating system, time.sleep() just means a hint. It's not a good timing mechanism, but good enough for most applications. Generally however, unless you want to sleep for a very small period, you can generally ignore this information.

import time sleep_start = time.time() for cnt in range(0,10): print(cnt) time.sleep(1) # 1 sec delay sleep_end = time.time() diff = sleep_end - sleep_start print("Looping time :",diff)
Looping time : 10.040287017822266