Pausing a script for a set amount of time is a common need — whether you’re slowing down a loop, waiting for a resource to become ready, or creating a visual effect like auto-typed text. Python’s built-in time module makes this straightforward with the sleep() function, though the right tool changes depending on whether your code is synchronous, threaded, or asynchronous.
Using time.sleep() for Basic Delays
The standard way to pause a script is time.sleep(), which suspends execution of the current thread for a given number of seconds.
import time
print("Starting...")
time.sleep(3)
print("...Finished")
The argument can be a float, so you can specify sub-second pauses like time.sleep(0.5) for half a second. This blocks the entire program during the pause, so nothing else runs until it’s done.
Adding Delays Inside a Loop
A common use case is pausing between iterations, such as printing characters one at a time to create a typing effect.
import time
message = "Hello"
for char in message:
print(char, end="")
time.sleep(0.5)
This prints each character with a half-second gap, useful for progress indicators or simple animations in the terminal.
Using asyncio.sleep() in Async Code
If your script runs inside an async def function, using time.sleep() blocks the entire event loop, which defeats the purpose of async code. asyncio.sleep() pauses only the current coroutine while letting other tasks continue.
import asyncio
async def main():
print("before")
await asyncio.sleep(2)
print("after")
asyncio.run(main())
Using Event.wait() in Threaded Code
For delays inside threads, threading.Event().wait() is often preferred over time.sleep() because it can be interrupted cleanly if needed, rather than blocking unconditionally.
import threading
event = threading.Event()
event.wait(timeout=3)
Scheduling a Delayed Function Call
If you need to delay a specific function rather than pausing the whole script, threading.Timer runs a function after a set number of seconds without blocking the rest of the program.
import threading
def greet():
print("Delayed hello!")
timer = threading.Timer(5.0, greet)
timer.start()
Which Method Should You Use?
For simple, linear scripts, time.sleep() is the easiest and most direct choice. Switch to asyncio.sleep() if you’re working with async/await code, and reach for Event.wait() or threading.Timer when delays need to coexist with other running threads. GUI applications are a special case — frameworks like Tkinter provide their own scheduling methods, such as .after(), to avoid freezing the interface.
A Common Pitfall
Using time.sleep() in a loop to “wait until something is ready” — like a file finishing a download or a job completing — tends to produce flaky code, since the right wait time depends on system load and timing that can vary between runs. It’s generally more reliable to poll for a condition or use a proper synchronization primitive instead of guessing with a fixed delay.
Join The Discussion
Time delays show up in all kinds of Python projects, from simple scripts to complex async pipelines. Have you run into issues with time.sleep() blocking code you didn’t expect, or found a clever use for delays in your own projects? Whether you’re just starting out with the time module or you’ve worked through the quirks of async delays, share your experiences, tips, or questions below — what’s your go-to approach, and where has it tripped you up?