Calling time.sleep() is the quickest way to pause Python code for a set number of seconds. For asynchronous code, use asyncio.sleep(), and for threads or GUIs prefer non-blocking waits so your app stays responsive.
Option 1: Use time.sleep() in synchronous code
import time
print("Starting...")
time.sleep(2) # sleep for 2 seconds
print("Done.")
time.sleep(0.1) # 100 milliseconds
for _ in range(5):
print("Tick")
time.sleep(1)
time.sleep() in a GUI’s main thread or inside an async event loop; use the options below to prevent freezing.Join readers who trust AllThings.How
Add us as a preferred source on Google so our practical guides show up first next time you search.
Add to Google Preferences →Option 2: Use asyncio.sleep() in async functions
asyncio.sleep() instead of blocking.import asyncio
async def work():
print("Hello ...")
await asyncio.sleep(1)
print("... world!")
asyncio.run(work())
import asyncio
async def step(name, delay):
await asyncio.sleep(delay)
print(f"{name} done")
async def main():
t1 = asyncio.create_task(step("A", 1))
t2 = asyncio.create_task(step("B", 2))
t3 = asyncio.create_task(step("C", 3))
await t1
await t2
await t3
asyncio.run(main())
Option 3: Use Event.wait() for responsive thread delays
With threads, waiting on an event makes shutdown immediate and avoids waiting for a blocking sleep to finish. See event objects in the documentation.
import threading
import time
stop = threading.Event()
time.sleep() with stop.wait(timeout) so the thread can exit immediately when signaled.def worker():
while not stop.is_set():
print("working...")
# Wait up to 1s, but return early if stop is set
stop.wait(1)
t = threading.Thread(target=worker, name="worker-1")
t.start()
time.sleep(3)
stop.set()
t.join()
Option 4: Schedule delays in Tkinter without freezing the UI
In Tkinter, use the widget’s after(ms, callback) method to run code later without blocking the event loop. Methods are listed in the Tkinter documentation.
import tkinter as tk
root = tk.Tk()
root.geometry("300x150")
def delayed():
print("Ran after 2 seconds")
root.after(2000, delayed)
root.mainloop()
Quick tips
time.sleep()blocks only the current thread; other threads continue running.- Use floats for fractional seconds, for example
time.sleep(0.25). - Prefer
asyncio.sleep()inside async code andEvent.wait()in threaded services for fast shutdowns. - In GUI apps, schedule work with timers (for example, Tkinter
after) to prevent frozen windows.
Choose the sleep primitive that matches your runtime: synchronous, async, thread-based, or GUI. You’ll get the delay you need without blocking the parts of your app that must stay responsive.






