Skip to content
Middle+0 views

How can inference requests be processed in parallel without fixed batching? What is a semaphore?

Practice

AI answer

Without fixed batching, each request can be sent for inference immediately, while concurrency is limited with a semaphore. A semaphore holds a limited number of permits: a task acquires a permit before the critical operation and releases it when finished. This limits the number of simultaneous computations and helps prevent overloading the model or GPU.

sem = asyncio.Semaphore(8)

async def infer(request):
    async with sem:
        return await model.predict(request)

This approach does not combine requests into a batch—it only limits concurrent processing. If better GPU utilization is needed, dynamic batching can be added: collect incoming requests up to a size limit or a short timeout. The semaphore limit is chosen based on load tests, latency, and available memory.