All the IPython Notebooks in Python Date Time Module lecture series by Dr. Milaan Parmar are available @ GitHub
sleep()
¶The sleep()
function suspends (waits) execution of the current thread for a given number of seconds.
Python has a module named time which provides several useful functions to handle time-related tasks. One of the popular functions among them is sleep()
.
The sleep()
function suspends execution of the current thread for a given number of seconds.
sleep()
¶# Example 1: Python sleep()
import time
print("Printed immediately.")
time.sleep(2.4)
print("Printed after 2.4 seconds.")
# When you run the program, the output will be like below:
Printed immediately. Printed after 2.4 seconds.
Explanation:
"Printed immediately"
is printed"Printed after 2.4 seconds"
is printed.As you can see from the above example, sleep()
takes a floating-point number as an argument.
Before Python 3.5, the actual suspension time may be less than the argument specified to the time()
function.
Since Python 3.5, the suspension time will be at least the seconds specified.
In the below program, we computed and printed the current local time inside the infinite while loop. Then, the program waits for 1 second. Again, the current local time is computed and printed. This process goes on.
# 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 below:
07:33:39 PM 07:33:40 PM 07:33:41 PM 07:33:42 PM 07:33:43 PM 07:33:44 PM 07:33:45 PM 07:33:46 PM 07:33:47 PM 07:33:48 PM 07:33:49 PM 07:33:50 PM 07:33:51 PM 07:33:52 PM 07:33:53 PM 07:33:54 PM 07:33:55 PM 07:33:56 PM 07:33:57 PM 07:33:58 PM 07:33:59 PM 07:34:00 PM 07:34:01 PM 07:34:02 PM 07:34:03 PM 07:34:04 PM 07:34:05 PM 07:34:06 PM 07:34:07 PM 07:34:08 PM 07:34:09 PM 07:34:10 PM 07:34:11 PM 07:34:12 PM 07:34:13 PM 07:34:14 PM 07:34:15 PM 07:34:16 PM 07:34:17 PM 07:34:18 PM 07:34:19 PM 07:34:20 PM 07:34:21 PM 07:34:22 PM 07:34:23 PM 07:34:24 PM 07:34:25 PM 07:34:26 PM
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-2-2f7f4e5fdb49> in <module> 7 result = time.strftime("%I:%M:%S %p", localtime) 8 print(result) ----> 9 time.sleep(1) 10 11 # When you run the program, the output will be something like below: KeyboardInterrupt:
Here is a slightly modified better version of the above program.
import time
while True:
localtime = time.localtime()
result = time.strftime("%I:%M:%S %p", localtime)
print(result, end="", flush=True)
print("\r", end="", flush=True)
time.sleep(1)
07:34:55 PM
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-3-76a0a5ce1450> in <module> 6 print(result, end="", flush=True) 7 print("\r", end="", flush=True) ----> 8 time.sleep(1) KeyboardInterrupt:
To learn more, visit digital clock in Python shell.
Before talking about sleep()
in multithreaded programs, let's talk about processes and threads.
A computer program is a collection of instructions. A process is the execution of those instructions.
A thread is a subset of the process. A process can have one or more threads.
All the programs above in this article are single-threaded programs. Here's an example of a multithreaded Python program.
# Example 3: Python multithreading
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()
# When you run the program, the output will be something like below:
Hello Hello Hello Hi Hi Hi
Explanation: The above program has two threads t1
and t2
. These threads are run using t1.start()
and t2.start()
statements.
Note: that,
t1
andt2
run concurrently and you might get different output.
Visit this page to learn more about Multithreading in Python.
time.sleep()
in multithreaded programs¶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.
sleep()
in a multithreaded program¶# 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()
Explanation: The above program has two threads. We have used time.sleep(0.5)
and time.sleep(0.75)
to suspend execution of these two threads for 0.5 seconds and 0.7 seconds respectively.
Recommended Reading: Python time.sleep() sleeps thread