Skip to main content

How to Pause a Python Program with time.sleep

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Sometimes a program should wait: before trying a request again, between checks, or to slow a loop down. time.sleep pauses for the number of seconds you give it, and fractions work too. This guide pauses for one and a half seconds and measures the wait, with real output from Python 3.15.2.

Syntax​

Syntax
import time

time.sleep(<seconds>)
PartWhat it means
<seconds>How long to pause, in seconds. A decimal such as 1.5 or 0.2 works

The time reference: "Suspend execution of the calling thread for the given number of seconds. The argument may be a non-integer to indicate a more precise sleep time."

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

time.perf_counter() reads a clock made for measuring durations, before and after the pause.

wait.py
import time

start = time.perf_counter()
time.sleep(1.5)
waited = time.perf_counter() - start

print(f"Waited {waited:.1f} seconds")

The wait.py script in a macOS-style code window: import time, start = time.perf_counter(), time.sleep(1.5), waited = time.perf_counter() - start, and a print of the waited seconds with one decimal.

Step 2: Run It​

Terminal
uv run python wait.py
Output
Waited 1.5 seconds

The script's output in a terminal: Waited 1.5 seconds.

The program paused for one and a half seconds before it printed.

Common Mistakes​

MistakeWhat happensDo this instead
Passing millisecondstime.sleep(1500) pauses for 1,500 seconds, 25 minutesDivide by 1,000: time.sleep(1.5)
A negative timeValueError: sleep length must be non-negativeKeep the time at 0 or more

Troubleshooting​

SymptomLikely causeFix
The program seems frozenA long sleep, or one inside a loopPrint a message before each pause so you can see the program is waiting

FAQ​

Can I stop a long sleep?​

Yes. Press Ctrl+C: in a 30-second sleep, Python stopped at once with a traceback ending in KeyboardInterrupt.

Why perf_counter and not time.time?​

The reference describes perf_counter as "a clock with the highest available resolution to measure a short duration". time.time follows the system clock, which "can return a lower value than a previous call if the system clock has been set back between the two calls".

Conclusion​

time.sleep(1.5) paused the program, and perf_counter measured Waited 1.5 seconds. Pass seconds, fractions included, and never a negative number.

Additional Resources​