Created
May 14, 2011 07:24
-
-
Save Artanis/972012 to your computer and use it in GitHub Desktop.
Function decorator to limit calls to the function to a given rate.
This file contains hidden or 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
| """Limit calls to a function to a given rate. | |
| Intended for use with functions that interact with systems not under the | |
| programmers control (for example, downloading html content from servers | |
| for parsing). | |
| usage: | |
| # Two rate limited functions | |
| # One limited to the default 3 second delay. | |
| @ratelimit() | |
| def limited_3s(): | |
| pass | |
| # The second limited to a 1 second delay. | |
| @ratelimit(1.0) | |
| def limited_1s(): | |
| pass | |
| # Tie two functions to the same limiter. | |
| # Calling these functions sequentially will result in a 3 second | |
| # delay between them. | |
| limiter = ratelimit() | |
| @limiter | |
| def tiedlimit1(): pass | |
| @limiter | |
| def tiedlimit2(): pass | |
| Copyright (c) 2011 Erik Youngren | |
| Permission 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. | |
| """ | |
| import time | |
| class ratelimit: | |
| """Limits the decorated functions to a given call rate. | |
| By default, this limit is once every 3 seconds (first call is | |
| unhindered), and checks the time every 0.5 seconds. | |
| Execution is blocked during this delay. Patches to support | |
| asynchronous are welcome. | |
| """ | |
| def __init__(self, delay=3.0, interval=0.5): | |
| self.delay = delay | |
| self.interval = interval | |
| self.last_call = 0.0 | |
| def __call__(self, func): | |
| def wrapper(*args, **kwargs): | |
| while time.time() - self.last_call < self.delay: | |
| time.sleep(self.interval) | |
| self.last_call = time.time() | |
| func(*args, **kwargs) | |
| return wrapper | |
| def main(): | |
| limiter = ratelimit(1.0) | |
| @limiter | |
| def x(): print('x') | |
| @limiter | |
| def y(): print('y') | |
| for i in range(2): | |
| x() | |
| y() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment