Thread Pool

import time
import concurrent.futures
from datetime import datetime


def sleepy_function(seconds):
    start_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(f"[{start_time}] Sleeping for {seconds} second(s)...")
    time.sleep(seconds)
    end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    return f"[{end_time}] Done sleeping for {seconds} second(s)"


if __name__ == "__main__":
    with concurrent.futures.ThreadPoolExecutor() as executor:
        seconds_list = [1, 3, 5, 2]  # Different sleep times
        results = executor.map(sleepy_function, seconds_list)

        for result in results:
            print(result)

Backlinks