Python time.sleep Function
Last modified April 11, 2025
This comprehensive guide explores Python's time.sleep
function,
which suspends execution for a given number of seconds. We'll cover basic
usage, timing control, and practical examples.
Basic Definitions
The time.sleep
function suspends execution of the current thread
for the given number of seconds. The argument can be a floating point number
for subsecond precision.
Key characteristics: platform-dependent accuracy (typically better than millisecond), non-busy waiting (releases CPU), and interruptible with KeyboardInterrupt (Ctrl+C).
Basic Sleep Example
The simplest use of time.sleep
pauses program execution.
This example shows basic usage with different delay durations.
import time print("Starting...") time.sleep(1) # Sleep for 1 second print("1 second has passed") time.sleep(0.5) # Sleep for 500 milliseconds print("500 milliseconds have passed") time.sleep(0.001) # Sleep for 1 millisecond print("1 millisecond has passed")
This example demonstrates how to pause execution for different durations. The sleep duration can be specified as an integer or floating-point number.
Note that actual sleep duration may vary slightly due to system scheduling and other factors.
Creating a Countdown Timer
time.sleep
can be used to create simple timers. This example
shows a countdown timer that prints remaining seconds.
import time def countdown(seconds): while seconds > 0: print(f"Time remaining: {seconds} seconds") time.sleep(1) seconds -= 1 print("Time's up!") countdown(5)
This pattern is useful for simple timing operations. The loop decrements the counter after each sleep interval.
For more precise timing, consider using time.perf_counter
to account for sleep inaccuracies.
Rate Limiting with Sleep
This example uses time.sleep
to implement rate limiting
for API calls or other operations that need throttling.
import time def make_api_call(): print("Making API call at", time.strftime("%H:%M:%S")) def rate_limited_call(calls_per_minute): interval = 60 / calls_per_minute while True: make_api_call() time.sleep(interval) # Limit to 10 calls per minute rate_limited_call(10)
The sleep duration is calculated based on the desired call rate. This ensures calls are spaced evenly over time.
For production use, consider adding error handling and exit conditions.
Creating a Simple Animation
time.sleep
can create simple text animations by controlling
the timing between frames. This example shows a spinning wheel animation.
import time def spinning_wheel(duration): frames = ['-', '\\', '|', '/'] end_time = time.time() + duration while time.time() < end_time: for frame in frames: print(f"\rLoading {frame}", end="", flush=True) time.sleep(0.1) print("\nDone!") spinning_wheel(5) # Run for 5 seconds
The animation works by cycling through frames with a small delay between each.
The \r
moves the cursor to the start of the line for overwriting.
This technique can be adapted for progress bars or other simple animations.
Simulating Processing Time
time.sleep
is often used to simulate processing time during
development. This example shows a mock file processing function.
import time import random def process_file(filename): print(f"Starting processing of {filename}") # Simulate variable processing time processing_time = random.uniform(0.5, 2.5) time.sleep(processing_time) print(f"Finished processing {filename} in {processing_time:.2f} seconds") return True files = ["data1.txt", "data2.txt", "data3.txt"] for file in files: process_file(file)
The random sleep duration simulates variable processing times. This is useful for testing systems that handle asynchronous operations.
In real applications, replace the sleep with actual processing code.
Creating a Polling Mechanism
This example uses time.sleep
to implement a polling loop
that checks for a condition at regular intervals.
import time def wait_for_condition(condition_func, timeout=30, interval=1): """ Wait for condition_func to return True or timeout. """ start_time = time.time() while time.time() - start_time < timeout: if condition_func(): return True time.sleep(interval) return False # Example usage: def check_file_exists(): # In real code, this would check filesystem return time.time() > start_time + 3 # Simulate file appearing after 3 seconds start_time = time.time() result = wait_for_condition(check_file_exists) print(f"Condition met: {result}")
The polling loop checks the condition at regular intervals while sleeping between checks. This prevents busy waiting that would consume CPU resources.
For production use, consider adding jitter to the interval to avoid thundering herd problems.
Best Practices
- Precision: Sleep duration is not guaranteed to be exact
- Alternatives: For precise timing, consider event-based approaches
- Interrupts: Sleep can be interrupted with KeyboardInterrupt
- Threading: Only the current thread is suspended
- GUI apps: Avoid sleep in main thread of GUI applications
Source References
Author
List all Python tutorials.