Course contentsShow
← Python
Lesson 868 of 1,06014. Concurrency and PerformanceFree lesson

Managing Thread-Safe Resource Pools with Semaphores

Use threading.Semaphore to restrict access to a finite set of shared resources, such as database connections or network sockets.

In this lesson, you will learn how to use a threading.Semaphore to limit the number of threads accessing a finite pool of shared resources, such as a set of database connections.

The Concept of a Resource Pool

In high-concurrency systems, you often face a constraint: you have many threads, but only a small number of physical resources (e.g., a connection pool limited to 5 slots). If you allow all threads to attempt to open a connection simultaneously, you risk overwhelming the database or crashing the service.

While a Lock provides exclusive access to one thread at a time, a Semaphore acts like a "bouncer" that maintains a counter. It allows a specific number of threads to pass through, while others must wait until a resource is returned.

Implementation with threading.Semaphore

You initialize the Semaphore with an integer representing the maximum number of concurrent permits available.

import threading
import time

# Allow only 3 threads to access the resource pool at once
pool_semaphore = threading.Semaphore(3)

def access_database(thread_id):
    print(f"Thread {thread_id} is waiting for a connection...")
    with pool_semaphore:
        print(f"Thread {thread_id} acquired a connection.")
        time.sleep(2)  # Simulating heavy I/O work
        print(f"Thread {thread_id} released the connection.")

# Spawning 10 threads, but only 3 will progress at a time
for i in range(10):
    threading.Thread(target=access_database, args=(i,)).start()

How it Works

  1. Acquiring: When a thread reaches the with pool_semaphore: block, it calls acquire(). If the internal counter is greater than zero, it decrements the counter and proceeds. If the counter is zero, the thread blocks until another thread releases a permit.
  2. Releasing: When the with block scope ends, release() is called automatically, incrementing the counter and signaling waiting threads that a resource has become available.

This approach ensures that your system stays within its configured capacity, preventing resource exhaustion during spikes in concurrent traffic without requiring complex state management.

Key Takeaway: Use a threading.Semaphore to enforce a hard limit on concurrent access to limited system resources, effectively balancing throughput and stability.