Package: asyncratelimiter
AsyncRateLimiterAsyncIO-enabled rate limiter - limit the rate of API calls!Public APIs such asWiktionarymay apply rate limits at which one may call any part of their API. A generous API will inform you of this rate limit through a well structured error. Other cases, however, may not be so graceous. It may respond with a general purpose status code 500; a generic, non-informative HTML error page; or even outright close the connection lacking any response whatsoever. For these cases, a rate limiter can help avoid triggering such a response altogether.AsyncRateLimiteris partly inspired byratelimiter. The main difference is thatAsyncRateLimiteris designed for asynchronous usage, whereasratelimiteris designed for sequential API calls - further wrap-around code is needed to enable asynchronous code.AsyncRateLimiterintegrates withAsyncIOfor convenience.InstallationSimply install viapip install asyncratelimiterDependenciesThere is a single dependency for convenience:requestsStylesAsyncRateLimitersupports two distinct usage styles:Threaded style - incorporates sequential APIs such asrequestswith rate-limited AsyncIOAsync style - limits calls to AsyncIO-based APIsThreaded StyleThreaded style revolves around theThreadedRateLimiter.dispatch(fun, /, *args, **kwargs)method.funis enqueued with Python's standardconcurrent.futures.ThreadPoolExecutor, which is particularly useful to dispatch multiple I/O-dependent tasks and waiting on their completion simultaneously.fromasyncratelimiterimportThreadedRateLimiterimportasyncioasyncdefmain():withThreadedRateLimiter(max_rate=10,interval=1)aslimiter:futures=[]for_inrange(100):futures.append(limiter.dispatch())asyncio.run(main())Requests IntegrationA simple derivation ofThreadedRateLimiterexposingget,post,put,delete, andrequestmethods; one may still accessThreadedRateLimiter.dispatchas normal.fromasyncratelimiterimportRequestLimiterimportasyncioasyncdefmain():futures=[]withRequestLimiter(max_rate=10,interval=2)asrequests:for_inrange(100):futures.append(requests.get('https://google.com'))awaitasyncio.gather(*futures)# Should take ~18-20 seconds - the last batch need not necessarily take the full 2s intervalasyncio.run(main())Async StyleAsync style behaves much likeratelimiter, except AsyncIO-integrated.fromasyncratelimiterimportAsyncRateLimiterimportasyncioasyncdeffoo():return42asyncdefmain():ratelimiter=AsyncRateLimiter(max_rate=10,interval=1)for_inrange(100):asyncwithratelimiter:awaitfoo()asyncio.run(main())For concurrently running tasks, async style still exposes adispatch(aw: Awaitable)method:fromasyncratelimiterimportAsyncRateLimiterimportasyncioasyncdeffoo(delay:float):awaitasyncio.sleep(delay)return42asyncdefmain():ratelimiter=AsyncRateLimiter(max_rate=10,interval=1)aws=[]for_inrange(100):aws.append(ratelimiter.dispatch(foo(0.5)))awaitasyncio.gatcher(*aws)LicenseMIT LicenseCopyright (c) 2021 KirusePermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.