How To Wait In Python (with example code)

Waiting is an important aspect of programming, as it allows you to pause the execution of your code for a certain amount of time. In Python, there are several ways to achieve this, each with its own set of advantages and disadvantages. In this article, we will explore some of the most common ways to wait in Python, and how to use them effectively in your own programs.

Using the time module

One of the most basic ways to wait in Python is to use the time module, which provides a range of functions for working with time and dates. To pause the execution of your program for a certain number of seconds, you can use the sleep() function, which takes a single argument: the number of seconds you want to pause for.

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

import time

print("Hello, world!")
print("Goodbye, world!")

This code will first print “Hello, world!”, then pause for 5 seconds, and then print “Goodbye, world!”. Note that sleep() is a blocking function, which means that the program will stop executing until the sleep period is over.

There are a few important things to keep in mind when using sleep(). First, the argument to sleep() is a floating point number, which means that you can use decimal values to specify sub-second delays. For example, time.sleep(0.5) will pause the program for half a second.

Second, sleep() is not accurate: the actual delay may be longer or shorter than the specified amount of time, depending on various factors such as system load and the resolution of the system clock. If you need precise timing, you may need to use a different method.

Using asyncio

If you’re using Python 3.5 or later, you can use the asyncio module to wait asynchronously. asyncio is a framework for writing asynchronous code using the async and await keywords.

To pause the execution of an asyncio program, you can use the asyncio.sleep() function, which is similar to the time.sleep() function we saw earlier. Here’s an example:

import asyncio

async def main():
    print("Hello, world!")
    await asyncio.sleep(5)
    print("Goodbye, world!")

This code works the same way as the time.sleep() example, but with one important difference: asyncio.sleep() is a non-blocking function, which means that it does not stop the execution of the program. Instead, it allows other tasks to run while the sleep period is in progress.

One advantage of using asyncio is that it allows you to write more efficient programs, because you can use the async and await keywords to write code that can be run concurrently with other tasks. However, asyncio can be more complex to use than the time module, and may not be necessary for simple programs.

Using threading

If you need to perform multiple tasks concurrently in your program, you may want to consider using the threading module. threading allows you to create separate threads of execution, each with its own code and variables.

To pause a thread in Python, you can use the time.sleep() function as we saw earlier, or you can use the threading.Event class, which allows you to pause and resume threads by setting and clearing a flag.

Here’s an example of how to use threading.Event to pause and resume a thread:

import threading

def worker(event):
    while True:
        print("Hello, world!")

event = threading.Event()
t = threading.Thread(target=worker, args=(event,))

# Pause the thread for 5 seconds

# Resume the thread

This code creates a thread that prints “Hello, world!” indefinitely. The main thread sets and clears an event flag to pause and resume the worker thread.

One advantage of using threading is that it allows you to perform multiple tasks concurrently, which can be useful for programs that need to perform long-running tasks or interact with external systems. However, threading can also be more complex to use than the time or asyncio modules, and requires careful management to avoid race conditions and other synchronization issues.

Using signal

If you need to pause your program in response to a signal from the operating system, you can use the signal module. signal allows you to register a function to be called when a certain signal is received, such as SIGINT (CTRL+C) or SIGALRM (alarm clock).

Here’s an example of how to use signal to pause the execution of your program:

import signal

def handler(signum, frame):
    print("Received signal", signum)

signal.signal(signal.SIGINT, handler)

print("Press CTRL+C to pause")

This code registers a handler function to be called when the SIGINT signal is received (e.g. when the user presses CTRL+C). When the signal is received, the signal.pause() function is called, which causes the program to pause until another signal is received.

One advantage of using signal is that it allows you to pause your program in response to external events, such as user input or system events. However, signal can be complex to use, and is not available on all platforms.


In this article, we have explored several ways to wait in Python, each with its own set of advantages and disadvantages. Whether you’re using the time module, asyncio, threading, or signal, you have a variety of options for pausing the execution of your code and controlling the flow of your program.


Table of Contents

Related posts