def rate_limited(max_per_second):
min_interval = <NUM_LIT:1.0> / float(max_per_second)<EOL>def decorate(func):<EOL><INDENT>last_time_called = [<NUM_LIT:0.0>]<EOL>rate_lock = threading.Lock()  <EOL>def rate_limited_function(*args, **kargs):<EOL><INDENT>try:<EOL><INDENT>rate_lock.acquire(True)<EOL>elapsed = time.clock() - last_time_called[<NUM_LIT:0>]<EOL>wait_time_remaining = min_interval - elapsed<EOL>if wait_time_remaining > <NUM_LIT:0>:<EOL><INDENT>time.sleep(wait_time_remaining)<EOL><DEDENT>last_time_called[<NUM_LIT:0>] = time.clock()<EOL><DEDENT>finally:<EOL><INDENT>rate_lock.release()<EOL><DEDENT>return func(*args, **kargs)<EOL><DEDENT>return rate_limited_function<EOL><DEDENT>return decorate<EOL>
This decorator limits how often a method can get called in a second.
        If the limit is exceeded, the call will be held in a queue until
        enough time has passed.
        Useful when trying to avoid overloading a system with rapid calls.