Content · Programming · Python

Python time.sleep() Function: Usage and Examples

Pause the current thread for a requested number of seconds.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Pause the current thread for a requested number of seconds.

How It Works


time.sleep(seconds) suspends the calling thread for at least approximately the specified duration, subject to scheduler delays. It blocks the thread; in an asyncio coroutine, use await asyncio.sleep() instead.

Step-by-Step Example


import time
started_at = time.perf_counter()
time.sleep(0.001)
elapsed_seconds = time.perf_counter() - started_at
print(elapsed_seconds >= 0)

Understanding the Example


  • The expression 'time.perf_counter()' supplies the value stored as 'started_at'. This value is then used or checked by the later statements.
  • The print() call exposes the result so you can compare it with the behavior described above. Check the value and its type when experimenting.

Run the complete example as written, then inspect the displayed result. The values returned by the operation are more important than simply seeing the script finish.

Where This Fits in a Real Program


For event timestamps, distinguish the displayed local clock from the instant that it represents. Store or exchange an unambiguous aware value, then convert it when displaying it to a user. For repeated calendar appointments, use a geographic timezone so daylight-saving rules are not lost.

Practice: Try It Yourself


sleep() does not replace a monotonic deadline for timeout logic.

For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.

Further Reading


Read the official Python documentation for complete parameters, return types and exceptions.

Common Beginner Mistakes


  • Assuming sleep() guarantees exact timing or putting blocking sleep in an async event loop.
  • Add a small controlled pause between simulated polling attempts.

Key Takeaways


  • Pause the current thread for a requested number of seconds.
  • time.sleep(seconds) suspends the calling thread for at least approximately the specified duration, subject to scheduler delays.
  • Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.
Share this resourceSend this page to someone who may find it useful.