Created
September 27, 2017 15:41
-
-
Save kai5263499/7efa11de355c6dad7aa954205b76c39b to your computer and use it in GitHub Desktop.
Python rate limit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def RateLimited(maxPerSecond): | |
minInterval = 1.0 / float(maxPerSecond) | |
def decorate(func): | |
lastTimeCalled = [0.0] | |
def rateLimitedFunction(*args,**kargs): | |
elapsed = time.clock() - lastTimeCalled[0] | |
leftToWait = minInterval - elapsed | |
if leftToWait>0: | |
time.sleep(leftToWait) | |
ret = func(*args,**kargs) | |
lastTimeCalled[0] = time.clock() | |
return ret | |
return rateLimitedFunction | |
return decorate | |
# Usage | |
# @RateLimited(0.0166) | |
# def vt_decorate(key, data): |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment