In this tutorial, we’ll be going over the Python time.sleep() method. In our previous tutorial, we looked at the time module, which is the default utility library for performing various timing tasks.
The Python time.sleep() method is used to halt the execution of the current program/thread for a given amount of time.
The current program/thread is essentially doing nothing in this time period, so it “sleeping” for that amount of time, before resuming from its current state.
Let’s take a look at how we can use this function.
Python time.sleep() Usage
This function is a part of the time
module hence we call it using the dot notation like time.sleep(). We must import the time module first.
import time
Now, to halt the execution of the program, we need to specify the number of seconds as an argument.
import time
num_seconds = 5
print('Going to sleep for', str(num_seconds), 'seconds')
time.sleep(num_seconds)
print('Woke up after', str(num_seconds), 'seconds')
Output
Going to sleep for 5 seconds
Woke up after 5 seconds
If you try this out on your machine, your program will halt for 5 seconds between the two outputs, since it is sleeping for that time.
We can also specify the number of seconds as a floating-point number, so we can sleep for 0.001
seconds (1 millisecond) or even 0.0000001
seconds (1 microsecond).
This will make the delay as precise as possible, within the floating-point and clock precision limits.
import time
num_millis = 2
print('Going to sleep for', str(num_millis), 'milliseconds')
time.sleep(num_millis / 1000)
print('Woke up after', str(num_millis), 'milliseconds')
Output
Going to sleep for 2 milliseconds
Woke up after 2 milliseconds
To measure the exact time of sleep, we can use the time.time()
method to start a timer. The difference between the start value and the end value of the timer will be our execution time.
Let’s test out our actual sleeping time in the above program.
import time
num_millis = 2
print('Going to sleep for', str(num_millis), 'milliseconds')
# Start timer
start_time = time.time()
time.sleep(num_millis / 1000)
# End timer
end_time = time.time()
print('Woke up after', str(end_time - start_time), 'seconds')
Output
Going to sleep for 2 milliseconds
Woke up after 0.0020711421966552734 seconds
Here, the time is not exactly 2 milliseconds, as you can see. It is about 2.071
milliseconds, which is slightly greater than it.
This is due to some delays in allocating resources, process scheduling, etc from the Operating System, which may cause a slight delay.
The extent of this delay will differ since you don’t know the exact state of the Operating System at a particular instance of time.
Variable time delay for time.sleep()
We can pass a variable to time.sleep()
, if you want a different amount of delay for some reason.
import time
delays = [1, 1.5, 2]
for delay in delays:
print('Sleeping for', delay, 'seconds')
time.sleep(delay)
Output
Sleeping for 1 seconds
Sleeping for 1.5 seconds
Sleeping for 2 seconds
Now that we’ve covered using time.sleep()
for our program, we can do the same for threads also.
Using Python time.sleep() on a Thread
This is a useful function in the context of multi-threading, as multiple threads may need to wait for a specific resource to be freed.
The below snippet shows how we can make multiple threads wait and print outputs, using Python time.sleep()
.
import time
from threading import Thread
class Worker(Thread):
# Entry point after thread.start() is invoked
def run(self):
for i in range(4):
print('Worker Thread', i)
time.sleep(i + 1)
class Waiter(Thread):
def run(self):
for i in range(10, 15):
print('Waiter thread', i)
time.sleep(i - 9)
print('Starting Worker Thread....')
Worker().start()
print('Starting Waiter Thread....')
Waiter().start()
print('Main thread finished!')
Output
Starting Worker Thread....
Worker Thread 0
Starting Waiter Thread....
Waiter thread 10
Main thread finished!
Worker Thread 1
Waiter thread 11
Worker Thread 2
Waiter thread 12
Worker Thread 3
Waiter thread 13
Waiter thread 14
Here, the execution of the main thread (program) is independent of the execution of the two threads. So, our main program finished first, before being followed by the Worker
and Waiter
threads.
Conclusion
In this article, we learned about using the Python time.sleep()
function in various ways.
References
- JournalDev article on time.sleep()
- Python Documentation