Python time.sleep() Method

The sleep() function in Python is used to pause the execution of the program for a specified number of seconds. The syntax for the sleep() function is as follows:

import time time.sleep(seconds)

Where seconds is the number of seconds to pause the execution of the program. For example, the following code will pause the execution of the program for 5 seconds:

How can I make a time delay in Python?

The Python sleep() method is employed to pause program execution for a specified duration (in seconds). It can be used to introduce a pause in the program's flow for a designated time period. The actual duration of suspension might be shorter if a signal interrupts it, while it could be extended due to other system activities. To introduce a delay in a Python script, one can pass the desired time in seconds 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 commonly hosted within processes, allowing multiple threads to coexist within a single process. These threads share the memory and state of the encompassing process, enabling them to collaborate and execute concurrently while accessing the same resources. 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()
output
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) function doesn't guarantee real-time accuracy. Instead, it relies on the underlying operating system's sleep() function, which might have slight variations in milliseconds. Many PCs have hardware limitations in the range of 1-10ms, regardless of the operating system. The operating system interprets time.sleep() as a suggestion, making it inadequate for precise timing, yet suitable for most practical purposes. In most scenarios, this information is negligible unless you're dealing with very short sleep periods.

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)
output
Looping time : 10.040287017822266

Conclusion

The time.sleep(seconds) function offers a way to introduce a pause in program execution for a specified duration. However, due to its reliance on the underlying operating system's sleep mechanism, it may not provide real-time accuracy, as hardware limitations and system scheduling can affect the actual delay. Despite its imprecision, time.sleep() remains suitable for most general applications where precise timing is not crucial.