Python is an interpreted high-level general-purpose programming language and python sleep() is one of the essential functions. The philosophical design of Python emphasizes code readability with the use of significant indentation. The language constructs as well as its object-oriented approach aim to help programmers write lear, logical code for small and large-scale projects.
Python is the dynamically typed and garbage collected language. It gives support to more than one programming paradigms, including structured, object-oriented, and functional programming. Python ranks as one of the most popular programming languages.
The sleep() function suspends the execution of the current thread for a particular number of seconds. There is a module in Python named time, it provides various useful functions that handle time-related tasks. sleep() is one of the very popular functions among them. It suspends the execution of the current thread for a given number of seconds.
Example to illustrate python sleep()
import time print("Printed immediately.") time.sleep(2.4) print("Printed after 2.4 seconds.")
Printed immediately. Printed after 2.4 seconds.
This is how the given program works:
- The program prints “Printed immediately”
- Suspends (Delays) execution for 2.4 seconds.
- later the program prints “Printed after 2.4 seconds” .
You can see in the above example that, sleep() takes a floating-point number as an argument. The actual suspension time was less than the argument specified to the time() function before Python 3.5.
Python sleep() Functions
- python sleep miliseconds: We can code a python program to sleep for some time, we can use the time.sleep() method. Here the argument takes in seconds. In order to use this method to sleep for milliseconds, you just use a fractional number. For example, to sleep for 600 millisecond use time.sleep(0.6), for 80 milliseconds use time.sleep(0.08) and so on.
- python sleep(0): Python time sleep(0) function is used to produce a delay in the execution of a code. We can use python sleep(0) function to halt the execution of the program for given time in seconds. Notice that python time sleep function actually stops the execution of current thread only, but not the whole program.
Example to demonstrate creation of digital clock using sleep()
import time while True: localtime = time.localtime() result = time.strftime("%I:%M:%S %p", localtime) print(result) time.sleep(1)
02:10:50 PM 02:10:51 PM 02:10:52 PM 02:10:53 PM 02:10:54 PM ... .. …
In the above program, the current local time has been computed inside the infinite while loop. Then, there will be a gap of one second and again the current local time will be computed and printed. This process will go on.
Multithreading using Python sleep()
Multithreading refers to concurrently executing multiple threads by rapidly switching the control of the CPU between threads, this is also called context switching. The Python Global Interpreter Lock limits one thread to run at a time even if the machine contains more than one processor.
Before talking about sleep() in a multithreaded program, here is something about processes and threads. Any computer program is a collection of instructions. Thread is the subset of processes that can have one or more threads.
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()
Hello Hello Hi Hello Hi Hi
This program has two threads t1 and t2. These threads runs using t1.start() and t2.start() statement.
But t1 and t2 has to run concurrently and we will get different outputs.