Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Mastering Python's time.sleep(): Best Practices, Multithreading, and Real‑World Examples

Python time.sleep()

time.sleep() pauses the current thread for a specified number of seconds.

Python’s time module offers a range of utilities for working with time. Among them, time.sleep() is essential for adding deliberate delays, synchronizing threads, or throttling operations.

Example 1: Basic Usage

import time
print("Immediately printed.")
time.sleep(2.4)
print("Printed after 2.4 seconds.")

This simple script demonstrates that time.sleep() accepts a floating‑point argument. In Python 3.5 and later, the function guarantees a minimum sleep duration; earlier versions may sleep slightly longer.

Example 2: Digital Clock

import time
while True:
    local = time.localtime()
    print(time.strftime("%I:%M:%S %p", local))
    time.sleep(1)

The loop prints the current local time every second. To avoid cluttering the terminal, the improved version updates the same line:

import time
while True:
    local = time.localtime()
    print(time.strftime("%I:%M:%S %p", local), end="", flush=True)
    print("\r", end="", flush=True)
    time.sleep(1)

See Python docs for time.sleep() for deeper insight.

Understanding Threads

A process is an executing program; threads are lightweight units within that process. Multiple threads can run concurrently, sharing the same memory space.

Example 3: Simple Multithreading

import threading
def greet_hello():
    for _ in range(3):
        print("Hello")
def greet_hi():
    for _ in range(3):
        print("Hi")
t1 = threading.Thread(target=greet_hello)
t2 = threading.Thread(target=greet_hi)
t1.start()
t2.start()

Because t1 and t2 execute concurrently, the output order can vary each run.

time.sleep() in Multithreaded Contexts

In single‑threaded programs, time.sleep() halts the entire process. In a multithreaded environment, only the calling thread is paused, allowing other threads to continue.

Example 4: Pausing Threads Differently

import threading, time
def say_hello():
    for _ in range(4):
        time.sleep(0.5)
        print("Hello")
def say_hi():
    for _ in range(4):
        time.sleep(0.7)
        print("Hi")
t1 = threading.Thread(target=say_hello)
t2 = threading.Thread(target=say_hi)
t1.start()
t2.start()

Here, one thread sleeps for 0.5 s while the other sleeps for 0.7 s, illustrating independent pacing.

For more on threading, see Python’s threading module.

Python

  1. Mastering Python Operators: A Comprehensive Guide
  2. Python List Operations: Creation, Access, Modification, and Advanced Techniques
  3. Mastering Python Tuples: Creation, Access, and Advanced Operations
  4. Mastering Python Dictionaries: Creation, Manipulation, and Advanced Techniques
  5. Retrieve Current Date and Time in Python: A Practical Guide
  6. Python: Retrieve Current Time and Timezone Data with Ease
  7. Mastering Python’s time Module: Functions, Structs, and Practical Examples
  8. Python timeit() – Measuring Execution Time with Practical Examples
  9. Python time.sleep(): How to Add Delays in Your Code (Example)
  10. Mastering Dates & Times in Python: A Practical Guide