pytoolbox.throttles module¶
Throttling classes implementing various throttling policies.
-
class
pytoolbox.throttles.
TimeThrottle
(min_time_delta)[source]¶ Bases:
object
Time based throttling class.
>>> import datetime >>> def slow_range(*args): ... for i in range(*args): ... time.sleep(0.5) ... yield i >>> t1, t2 = (TimeThrottle(t) for t in (datetime.timedelta(minutes=1), 0.2)) >>> list(t1.throttle_iterable((i, i) for i in range(10))) [(0, 0), (9, 9)] >>> list(t2.throttle_iterable(slow_range(3))) [0, 1, 2]
-
class
pytoolbox.throttles.
TimeAndRatioThrottle
(min_ratio_delta, min_time_delta, max_time_delta)[source]¶ Bases:
pytoolbox.throttles.TimeThrottle
Time and ratio based throttling class.
>>> import datetime >>> def slow_range(*args): ... for i in range(*args): ... time.sleep(0.5) ... yield i >>> t1, t2 = (TimeAndRatioThrottle(0.3, t, 10*t) for t in (datetime.timedelta(minutes=1), 0.4)) >>> list(t1.throttle_iterable(list(range(9)), lambda i: [i/9])) [0, 8] >>> list(t2.throttle_iterable(slow_range(9), lambda i: [i/9])) [0, 3, 6, 8]