Package: throttler
ThrottlerZero-dependency Python package for easy throttling with asyncio support.📝 Table of Contents🎒Install🛠Usage ExamplesThrottler and ThrottlerSimultaneousSimple ExampleAPI ExampleExecutionTimerTimer👨🏻‍💻Author💬Contributing📝License🎒 InstallJustpipinstallthrottler🛠 Usage ExamplesAll run-ready examples arehere.Throttler and ThrottlerSimultaneousThrottler:Context manager for limiting rate of accessing to context block.fromthrottlerimportThrottler# Limit to three calls per secondt=Throttler(rate_limit=3,period=1.0)asyncwitht:passOrimportasynciofromthrottlerimportthrottle# Limit to three calls per second@throttle(rate_limit=3,period=1.0)asyncdeftask():returnawaitasyncio.sleep(0.1)ThrottlerSimultaneous:Context manager for limiting simultaneous count of accessing to context block.fromthrottlerimportThrottlerSimultaneous# Limit to five simultaneous callst=ThrottlerSimultaneous(count=5)asyncwitht:passOrimportasynciofromthrottlerimportthrottle_simultaneous# Limit to five simultaneous calls@throttle_simultaneous(count=5)asyncdeftask():returnawaitasyncio.sleep(0.1)Simple Exampleimportasyncioimporttimefromthrottlerimportthrottle# Limit to two calls per second@throttle(rate_limit=2,period=1.0)asyncdeftask():returnawaitasyncio.sleep(0.1)asyncdefmany_tasks(count:int):coros=[task()for_inrange(count)]forcoroinasyncio.as_completed(coros):_=awaitcoroprint(f'Timestamp:{time.time()}')asyncio.run(many_tasks(10))Result output:Timestamp: 1585183394.8141203
Timestamp: 1585183394.8141203
Timestamp: 1585183395.830335
Timestamp: 1585183395.830335
Timestamp: 1585183396.8460555
Timestamp: 1585183396.8460555
...API ExampleimportasyncioimporttimeimportaiohttpfromthrottlerimportThrottler,ThrottlerSimultaneousclassSomeAPI:api_url='https://example.com'def__init__(self,throttler):self.throttler=throttlerasyncdefrequest(self,session:aiohttp.ClientSession):asyncwithself.throttler:asyncwithsession.get(self.api_url)asresp:returnrespasyncdefmany_requests(self,count:int):asyncwithaiohttp.ClientSession()assession:coros=[self.request(session)for_inrange(count)]forcoroinasyncio.as_completed(coros):response=awaitcoroprint(f'{int(time.time())}| Result:{response.status}')asyncdefrun():# Throttler can be of any typet=ThrottlerSimultaneous(count=5)# Five simultaneous requestst=Throttler(rate_limit=10,period=3.0)# Ten requests in three secondsapi=SomeAPI(t)awaitapi.many_requests(100)asyncio.run(run())Result output:1585182908 | Result: 200
1585182908 | Result: 200
1585182908 | Result: 200
1585182909 | Result: 200
1585182909 | Result: 200
1585182909 | Result: 200
1585182910 | Result: 200
1585182910 | Result: 200
1585182910 | Result: 200
...ExecutionTimerContext manager for time limiting of accessing to context block. Simply sleepperiodsecs before next accessing, not analog ofThrottler. Also it can align to start of minutes.importtimefromthrottlerimportExecutionTimeret=ExecutionTimer(60,align_sleep=True)whileTrue:withet:print(time.asctime(),'|',time.time())Orimporttimefromthrottlerimportexecution_timer@execution_timer(60,align_sleep=True)deff():print(time.asctime(),'|',time.time())whileTrue:f()Result output:Thu Mar 26 00:56:17 2020 | 1585173377.1203406
Thu Mar 26 00:57:00 2020 | 1585173420.0006166
Thu Mar 26 00:58:00 2020 | 1585173480.002517
Thu Mar 26 00:59:00 2020 | 1585173540.001494TimerContext manager for pretty printing start, end, elapsed and average times.importrandomimporttimefromthrottlerimportTimertimer=Timer('My Timer',verbose=True)for_inrange(3):withtimer:time.sleep(random.random())Orimportrandomimporttimefromthrottlerimporttimer@timer('My Timer',verbose=True)deff():time.sleep(random.random())for_inrange(3):f()Result output:#1 | My Timer | begin: 2020-03-26 01:46:07.648661
#1 | My Timer |   end: 2020-03-26 01:46:08.382135, elapsed: 0.73 sec, average: 0.73 sec
#2 | My Timer | begin: 2020-03-26 01:46:08.382135
#2 | My Timer |   end: 2020-03-26 01:46:08.599919, elapsed: 0.22 sec, average: 0.48 sec
#3 | My Timer | begin: 2020-03-26 01:46:08.599919
#3 | My Timer |   end: 2020-03-26 01:46:09.083370, elapsed: 0.48 sec, average: 0.48 sec👨🏻‍💻 AuthorRamzan Bekbulatov:Telegram:@rm_bkGithub:@uburuntu💬 ContributingContributions, issues and feature requests are welcome!📝 LicenseThis project isMITlicensed.