Skip to content

Effortlessly Waiting in Python: A Step-by-Step Guide

Python sleep(): How to Add Time Delays to Your Code

Note: This article is intended for intermediate developers who are looking to grow their knowledge of Python.

Have you ever needed to make your Python program wait for something? Most of the time, you’d want your code to execute as quickly as possible. But there are times when letting your code sleep for a while is actually in your best interest.

For example, you might use a Python sleep() call to simulate a delay in your program. Perhaps you need to wait for a file to upload or download, or for a graphic to load or be drawn to the screen. You might even need to pause between calls to a web API, or between queries to a database. Adding Python sleep() calls to your program can help in each of these cases, and many more!

In this tutorial, we’ll learn how to add Python sleep() calls with:

  • time.sleep()
  • Decorators
  • Threads
  • Async IO
  • Graphical User Interfaces

Adding a Python sleep() Call With time.sleep()

Python has built-in support for putting your program to sleep. The time module has a function sleep() that you can use to suspend execution of the calling thread for however many seconds you specify.

Here’s an example of how to use time.sleep():

import time
time.sleep(3) # Sleep for 3 seconds

If you run this code in your console, then you should experience a delay before you can enter a new statement in the REPL.

Note: In Python 3.5, the core developers changed the behavior of time.sleep() slightly. The new Python sleep() system call will last at least the number of seconds you’ve specified, even if the sleep is interrupted by a signal. This does not apply if the signal itself raises an exception, however.

You can test how long the sleep lasts by using Python’s timeit module:

Terminal window
$ python3 -m timeit -n 3 "import time; time.sleep(3)"
3 loops, best of 5: 3 sec per loop

Here, you run the timeit module with the -n parameter, which tells timeit how many times to run the statement that follows. You can see that timeit ran the statement 3 times and that the best run time was 3 seconds, which is what was expected.

The default number of times that timeit will run your code is one million. If you were to run the above code with the default -n, then at 3 seconds per iteration, your terminal would hang for approximately 34 days! The timeit module has several other command line options that you can check out in its documentation.

Let’s create something a bit more realistic. A system administrator needs to know when one of their websites goes down. You want to be able to check the website’s status code regularly, but you don’t want to overload the server with too many requests. So, you decide to add a delay between each status code check. Here’s how you can do it:

import requests
import time
def check_website_status(url):
while True:
response = requests.get(url)
if response.status_code != 200:
print("Website is down!")
print("Website is up and running.")
time.sleep(60) # Delay for 60 seconds before next check
url = "https:"

In this example, we define a function check_website_status() that continuously checks the status code of the given URL using the requests library. If the status code is not 200, we print a message indicating that the website is down. Otherwise, we print a message indicating that the website is up and running. After each check, we add a delay of 60 seconds using time.sleep().

By adding this delay, you ensure that you’re not bombarding the website with requests too frequently, which can be seen as suspicious behavior or even cause the server to crash.

That’s how you can use time.sleep() to add time delays to your Python code. The sleep() function is a simple and effective way to introduce pauses in your program and control the timing of certain operations.

In the next sections, we’ll explore more advanced techniques for adding delays to your Python code, such as using decorators, threads, async IO, and graphical user interfaces.

Adding a Python sleep() Call With Decorators

[To be continued…]