"""Decorators."""
-import datetime
import enum
import functools
import inspect
import threading
import time
import traceback
-from typing import Callable, Optional
+from typing import Any, Callable, Optional
import warnings
# This module is commonly used by others in here and should avoid
return wrapper_invocation_logged
+def rate_limited(n_per_second: int) -> Callable:
+ """Limit invocation of a wrapped function to n calls per second.
+ Thread safe.
+
+ """
+ min_interval = 1.0 / float(n_per_second)
+
+ def wrapper_rate_limited(func: Callable) -> Callable:
+ last_invocation_time = [0.0]
+
+ def wrapper_wrapper_rate_limited(*args, **kargs) -> Any:
+ while True:
+ elapsed = time.clock_gettime(0) - last_invocation_time[0]
+ wait_time = min_interval - elapsed
+ if wait_time > 0.0:
+ time.sleep(wait_time)
+ else:
+ break
+ ret = func(*args, **kargs)
+ last_invocation_time[0] = time.clock_gettime(0)
+ return ret
+ return wrapper_wrapper_rate_limited
+ return wrapper_rate_limited
+
+
def debug_args(func: Callable) -> Callable:
"""Print the function signature and return value at each call."""