Skip to content

Instantly share code, notes, and snippets.

@kai5263499
Created September 27, 2017 15:41
Show Gist options
  • Save kai5263499/7efa11de355c6dad7aa954205b76c39b to your computer and use it in GitHub Desktop.
Save kai5263499/7efa11de355c6dad7aa954205b76c39b to your computer and use it in GitHub Desktop.
Python rate limit
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