Unlocking the power of asyncio Semaphore

When building asynchronous applications, oftentimes you need to limit the number of simultaneous connections to a shared resource. It can be your internal server, or an API that has usage limits. asyncio library provides a dedicated synchronization primitive Semaphore created exactly for this purpose. However, let’s first try to solve this problem without using it, in order to fully appreciate the value of this mechanism. We can limit the number of simultaneous connections by using a counter variable that will be incremented whenever we start making a request and decremented when we receive our response. Let’s look at the example code: ...

May 31, 2023