Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Semaphore

Семафор

A synchronization mechanism that limits how many tasks can use a shared resource at once. Each task needs a permit to proceed and waits if none are available.

What a semaphore does

Suppose a service downloads files from a provider that allows only five active downloads per client. The service can accept more than five jobs without starting all their downloads at once. A semaphore with five permits lets the first five jobs through and makes the rest wait. As soon as a job finishes and returns its permit, another can start. The semaphore limits only the work between acquiring and releasing a permit, not everything the service does.

How permits work

A counting semaphore tracks the number of available permits. An acquire operation checks and takes a permit atomically, so two callers cannot both claim the last one. If none is available, the normal acquire operation waits. Depending on the API, that may mean blocking an operating system thread or suspending an async task so other tasks can run. A release makes a permit available again. Don't assume that the longest-waiting caller gets it first unless the API guarantees that order.

A binary semaphore has either zero or one permit available. Like a mutex, it can let only one caller into a section at a time, but it usually has no owner. A mutex is meant to be unlocked by its owner; a semaphore can be released by a different task. That makes it useful for signaling: with zero initial permits, a consumer can wait for a producer to signal that work is ready.

Example in Python

import asyncio

download_slots = asyncio.Semaphore(5)

async def fetch_limited(client, url):
    async with download_slots:
        return await client.get(url)

Here, coroutines on the same event loop share one semaphore. Entering the async with block acquires a permit, and leaving it returns the permit even if client.get raises an exception. Creating a new semaphore inside every call would give each caller its own set of permits and impose no shared limit. If the client streams the body after get returns, keep the permit until the body is consumed or the response is closed.

Where it fits

Semaphores are useful for capping active network requests, memory-intensive jobs, or access to a fixed set of resources. They don't hand out the resources themselves: a connection pool, for example, still has to choose and return a specific connection. A semaphore is also not a rate limiter. Five permits can cap active requests at five, but if those requests finish quickly, many more than five can start in one second.

What interviewers ask

A common prompt is to run 100 jobs while keeping at most five requests in flight. Explain where you'd put the shared semaphore, how long each job holds its permit, and how you'd return it on failure. A missing release reduces capacity and can eventually leave all remaining callers stuck. An extra release can silently raise the limit in implementations that don't enforce a maximum, which is one reason to prefer a context manager or a try/finally block over scattered release calls.