From 9d0e4a331bb80941e70d5a5f44445f8b165cc45b Mon Sep 17 00:00:00 2001 From: Balint Vuchetich Date: Thu, 2 Nov 2017 11:02:49 +0100 Subject: [PATCH] add rate limit example --- process_management/rate_limiting.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 process_management/rate_limiting.py diff --git a/process_management/rate_limiting.py b/process_management/rate_limiting.py new file mode 100644 index 0000000..366647c --- /dev/null +++ b/process_management/rate_limiting.py @@ -0,0 +1,27 @@ +from ratelimiter import RateLimiter + +@RateLimiter(max_calls=1, period=1) +def foo(i): + print(i) + +@RateLimiter(max_calls=5, period=10) +def bar(i): + foo(i) + + + + +if __name__ == '__main__': + + # with decorator: + for i in range(100): + bar(i) + + # with context: + rate_limiter1 = RateLimiter(max_calls=1, period=1) + rate_limiter2 = RateLimiter(max_calls=5, period=10) + + for i in range(100): + with rate_limiter1: + with rate_limiter2: + print(i) \ No newline at end of file