I got restricted by a third party API provider. It is supposed to be called 10 requests per second, but I have called 800 requests per second. I totally agree with my mistake.
I needed to resolve this problem. Controlling the speed of calling API was the main point in this situation. And also, in this case, the caller was implemented using asyncio. In summary, controlling the concurrent calling API was the main point.
I checked Primitive types on asyncio first because it supports built-in Lock and built-in Semaphore. It was easy to use them so I tried using that.
But, the real problem was the concurrent tasks were run on distributed environments. It was impossible to use Python built-in since I had to deal with multiple nodes.
Fortunately, I could find a library called aioredlock. It was the lock implementation for Redis. The abstraction was perfect so what I did was using the simple interfaces such as lock(), is_locked().
I was going to attach an example code myself but it would be good to check the example code on the repository yourself because it is so easy.
I locked it when an API call has been started and released it after the call is ended.
There might be good ways to implement Distributed Lock. I have written it for introducing an easy way. I hope this helps or will help you. Thank you for reading it.