liveBook · Manning

Semaphore

A semaphore is a synchronization primitive that maintains a count of available permits to limit or coordinate concurrent access to a resource or operation.

How 5 Manning books explain it

Python Concurrency with asyncio

A semaphore is a synchronization primitive that maintains a counter and permits a bounded number of execution contexts to access a resource concurrently.

How it is used
Limits concurrency and controls access to finite resources such as connection pools, APIs, CPUs, and subprocesses.
How deep it goes
Explains permit counting, blocking at zero, lock equivalence, async usage, and excess-release detection through BoundedSemaphore.
Examples
Uses asyncio tasks, limited API requests, database connections, and CPU-bound gpg subprocesses as concrete applications.
What you can do after
Apply asyncio.Semaphore or BoundedSemaphore to constrain concurrent operations and diagnose incorrect manual release logic.

Read how Python Concurrency with asyncio explains semaphore →

Software Design for Python Programmers

A Python Semaphore object controls concurrent access by maintaining a counter of available permits.

How it is used
Coordinates multithreaded access when multiple readers may share a resource but writers require exclusion.
How deep it goes
Explains permit counting, blocking, releasing, and reader-writer coordination through a worked Python simulation; defers broader performance concerns.
Examples
Uses a meter, technician writer threads, logger reader threads, Lock, Semaphore(3), simulated delays, and timestamped output.
What you can do after
Apply it via Python's threading module to limit simultaneous readers and protect shared resources from concurrent writes.

Read how Software Design for Python Programmers explains semaphore →

Learn Concurrent Programming with Go

A synchronization mechanism that controls concurrent access to a resource through a counter of available permits.

How it is used
Limits concurrent access to shared resources and records completion signals between goroutines.
How deep it goes
Implements permit accounting with condition variables, including Acquire and Release behavior, then applies it to completion and channel buffering.
Examples
Uses a limited database, a zero-permit task-completion signal, and capacity-controlled channel sending.
What you can do after
Build it from scratch with a permit counter and condition variable, then apply it to bounded access and completion signaling.

Read how Learn Concurrent Programming with Go explains semaphore →

Build an AI Agent (From Scratch)

A semaphore is a synchronization primitive that limits how many concurrent operations may enter a protected region.

How it is used
Limits simultaneous LLM API calls while many asynchronous evaluation tasks run concurrently.
How deep it goes
Implements `asyncio.Semaphore` around `acompletion` and extends it to provider-specific limits; does not examine semaphore internals.
Examples
Shows 100 tasks capped at 10 API calls, then separate OpenAI and Anthropic limits of 30 and 10.
What you can do after
Operate/configure it by wrapping LiteLLM calls in `async with` and adjusting provider-specific limits when rate-limit errors occur.

Read how Build an AI Agent (From Scratch) explains semaphore →

Grokking Concurrency

A semaphore is a synchronization primitive that controls access to a resource while allowing multiple tasks to hold access concurrently.

How it is used
Controls concurrent access to a bounded shared resource while permitting several tasks to proceed simultaneously.
How deep it goes
Explains counters, blocking, waking, acquire-release behavior, mutex comparison, and a threaded Python implementation.
Examples
Uses a three-space parking garage, ten threads, parking permits, acquire-release operations, and a protected parked-car list.
What you can do after
Apply semaphores in Python to limit concurrent resource users and coordinate waiting tasks.

Read how Grokking Concurrency explains semaphore →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.