HOWTO · Python

How to Wait 5 Seconds in Python

This tutorial discusses how to wait 5 seconds in Python.

On this page

For ordinary synchronous Python code, call time.sleep(5) to pause the current thread for about five seconds. Use threading.Event when another thread may signal an early wake-up, threading.Timer for a delayed callback, asyncio.sleep() inside a coroutine, or sched when you need to run scheduled events.

Use the time.sleep() Function to Wait 5 Seconds in Python

The time.sleep() function takes a duration in seconds and suspends the calling thread. The duration can be fractional, such as 0.5, but five seconds is a requested minimum delay rather than a promise of an exact wake-up time; operating-system scheduling can make the actual pause longer.

This standalone example prints a message before and after the pause:

import time

print("Waiting")
time.sleep(5)
print("Finished")
Waiting
Finished

Use this approach for a simple synchronous script. While the thread is sleeping it cannot do other work. A normal keyboard interrupt such as Ctrl+C can interrupt the wait; custom signal handling is usually unnecessary for this task.

Use the threading Library With the Event Class to Wait 5 Seconds in Python

threading.Event is a thread coordination primitive. Calling wait(5) blocks the calling thread for up to five seconds and returns early if another thread sets the event first. This makes it useful when a worker should wait for either a signal or a timeout.

import threading

ready = threading.Event()
print("Waiting")
ready.wait(5)
print("Finished")
Waiting
Finished

No other thread sets ready in this example, so it waits for the timeout. In a coordinated program, another thread can call ready.set() to wake the waiter; check the return value of wait() if your next action depends on whether the event was set before the timeout.

Use the threading Library With the Timer() Function to Wait 5 Seconds in Python

threading.Timer is a class that starts a separate thread and invokes a callback after a delay. It does not pause the thread that starts it. Keep the timer alive with join() when a short script should wait for the callback to finish; call cancel() if the callback is no longer needed before it starts.

from threading import Timer


def report():
    print("Callback ran")


print("Timer started")
timer = Timer(5, report)
timer.start()
timer.join()
print("Main thread finished")
Timer started
Callback ran
Main thread finished

The callback runs on the timer thread, while join() makes the main thread wait for that thread to finish. A timer schedules one callback; use a loop or a scheduler for repeated or multiple events.

Use asyncio.sleep() for Asynchronous Code to Wait 5 Seconds in Python

Inside asynchronous code, await asyncio.sleep(5) suspends the current coroutine while allowing the event loop to run other tasks. It does not block the event-loop thread like time.sleep(5) would.

import asyncio


async def main():
    print("Waiting")
    await asyncio.sleep(5)
    print("Finished")


asyncio.run(main())
Waiting
Finished

Use asyncio.run(main()) to start this standalone script. In an application that already has a running event loop, call or schedule the coroutine within that loop instead of starting another one with asyncio.run().

Use the sched Module to Wait 5 Seconds in Python

The sched module provides a general-purpose event scheduler. Add an event with a delay, priority, callback, and optional arguments, then call run() to process the queue. If the next event is in the future, run() waits until it is due; this is useful for managing scheduled events, though it is more machinery than a one-time pause.

import sched
import time


def report():
    print("Scheduled event ran")


scheduler = sched.scheduler(time.monotonic, time.sleep)
print("Scheduler started")
scheduler.enter(5, 1, report)
scheduler.run()
print("Scheduler finished")
Scheduler started
Scheduled event ran
Scheduler finished

The scheduler uses a monotonic clock so elapsed-time scheduling is not affected by wall-clock adjustments. Its delay function is time.sleep, and run() does not return until the queued event has run.

Conclusion

Choose time.sleep(5) for a straightforward synchronous pause. Use threading.Event.wait(5) when another thread may signal the waiter, threading.Timer to run one callback later, asyncio.sleep(5) within a coroutine so other tasks can progress, and sched to manage one or more timed events. Each duration is approximate because the operating system and event loop determine when waiting code runs again.