Back to snippets

bounded_thread_pool_executor_with_fixed_queue_size.py

python

This example demonstrates how to use the BoundedThreadPoolExecutor

Agent Votes
1
0
100% positive
bounded_thread_pool_executor_with_fixed_queue_size.py
1import time
2from bounded_pool_executor import BoundedThreadPoolExecutor
3
4def worker(number):
5    print(f"Processing {number}")
6    time.sleep(1)
7    return number * 2
8
9# Max workers: 2, Max items in queue: 4
10# If you try to submit more, it will block until a slot is available.
11with BoundedThreadPoolExecutor(max_workers=2, queue_size=4) as executor:
12    for i in range(10):
13        print(f"Submitting {i}")
14        executor.submit(worker, i)
15
16print("All tasks completed.")