instapy/time_util.py
def rate_limited(max_per_hour: int):
    """Rate-limits the decorated function locally, for one process."""
    lock = threading.Lock()
    min_interval = 3600 / max_per_hour

    def decorate(func):
        last_time_called = time.perf_counter()

        @wraps(func)
        def rate_limited_function(*args, **kwargs):
            lock.acquire()
            nonlocal last_time_called
            try:
                elapsed = time.perf_counter() - last_time_called
                left_to_wait = min_interval - elapsed
                if left_to_wait > 0:
                    time.sleep(left_to_wait)

                return func(*args, **kwargs)
            finally:
                last_time_called = time.perf_counter()
                lock.release()

        return rate_limited_function

    return decorate
Rate-limits the decorated function locally, for one process.
Rate-limits the decorated function locally, for one process.
Rate - limits the decorated function locally for one process .
def rate_limited(max_per_hour: int):
    lock = threading.Lock()
    min_interval = 3600 / max_per_hour
    def decorate(func):
        last_time_called = time.perf_counter()
        @wraps(func)
        def rate_limited_function(*args, **kwargs):
            lock.acquire()
            nonlocal last_time_called
            try:
                elapsed = time.perf_counter() - last_time_called
                left_to_wait = min_interval - elapsed
                if left_to_wait > 0:
                    time.sleep(left_to_wait)
                return func(*args, **kwargs)
            finally:
                last_time_called = time.perf_counter()
                lock.release()
        return rate_limited_function
    return decorate
def rate_limited ( max_per_hour : int ) : lock = threading . Lock ( ) min_interval = 3600 / max_per_hour def decorate ( func ) : last_time_called = time . perf_counter ( ) @ wraps ( func ) def rate_limited_function ( * args , ** kwargs ) : lock . acquire ( ) nonlocal last_time_called try : elapsed = time . perf_counter ( ) - last_time_called left_to_wait = min_interval - elapsed if left_to_wait > 0 : time . sleep ( left_to_wait ) return func ( * args , ** kwargs ) finally : last_time_called = time . perf_counter ( ) lock . release ( ) return rate_limited_function return decorate
Rate-limits the decorated function locally, for one process.
Rate - limits the decorated function locally for one process .
"""Rate-limits the decorated function locally, for one process."""
max_per_hour
int