In Python, or any other programming language, sometimes you want to add a time delay in your code before you proceed to the next section of the code.
If this is what you want to do, then you should use the sleep function from the time module.
First, I will start by discussing how to use Python’s sleep function. After that I will talk more about some frequently asked questions and how the sleep function is actually implemented under the hood.
Like I mentioned, Sleep is a Python built-in function in the time module.
So in order to use sleep, you will need to import the time module first.
sleep takes only one argument which is the period of time in seconds that you want to sleep for.
Using the sleep function in Python 2 and Python 3 is exactly the same, so you won’t need to worry about which version of Python your code is running on.
It is really very simple and straight-forward. Let’s walk through some examples.
Let’s say you want to use sleep to add a 10-second delay.
In the following example, I print out the elapsed time between invoking the sleep function and after it returns.
import time # record start time start_time = time.time() # sleep for 10 seconds time.sleep(10) # print elapsed time elapsed_time = time.time() - start_time print(elapsed_time) # output: 10.000149965286255
As you can see, the elapsed time is very close to 10 seconds which is what we expect.
No changes in Python 3.
You can use the sleep function in Python 2 exactly as you do in Python 2.
Frequently Asked Questions
Q: How to use the sleep function to sleep for a time period that is less than a second, for example milliseconds or microseconds?
The sleep function actually takes a floating point number as an argument, not an integer.
And that means, you can pass any fraction of a second as an argument.
import time # sleep for half a second time.sleep(0.5) # sleep for 1 millisecond time.sleep(1 * 10**(-3)) # sleep for 1 microsecond time.sleep(1 * 10**(-6))
Q: How to use the sleep function to sleep until a specific time?
This is a very common question.
Depending on what you are trying to do, there are two different ways to achieve this.
The straight-forward way is to precompute the delay (in seconds) until that specific time before you call the sleep function.
Another way is to use the event.wait function from the event module.
If what you are trying to do is to block a thread until a condition happens, it is much better to use wait().
Q: [Advanced] How is sleep actually implemented?
This is a fantastic question.
sleep is not actually implemented by Python.
Well, it is, but it is simply a wrapper around something else that is implemented by the Operating System.
All Operating Systems have a Sleep() system call.
If you don’t know what a System Call is, then you should!
But either way, you can think of a System Call as an API or an interface that the OS provides for user-space programs to interact with the OS.
So what does an OS do when it receives a Sleep System Call?
Basically what the OS will do is that it will suspend the process (your program) from being scheduled on the CPU for the time period that you specified.
Notice that this is completely different from adding delay using a dummy for loop that does nothing (which is something you should NEVER do).
In the case of a dummy for loop, your process is actually still running on the CPU.
But in the case of Sleep(), your process will be effectively dormant for a while until the OS starts scheduling it again on the CPU.
Check out the Courses section!
- The Python Learning Path (From Beginner to Mastery)
- Learn Computer Science (From Zero to Hero)
- Coding Interview Preparation Guide
- The Programmer’s Guide to Stock Market Investing
- How to Start Your Programming Blog?
Are you Beginning your Programming Career?
I provide my best content for beginners in the newsletter.
- Python tips for beginners, intermediate, and advanced levels.
- CS Career tips and advice.
- Special discounts on my premium courses when they launch.
And so much more…
Subscribe now. It’s Free.