-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy paththrottle.py
More file actions
72 lines (59 loc) · 1.94 KB
/
throttle.py
File metadata and controls
72 lines (59 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Module containing the helper class for Throttling.
"""
from collections.abc import Callable, Generator
from contextlib import contextmanager
from functools import wraps
from threading import Semaphore
from typing import ParamSpec, TypeVar
P = ParamSpec("P")
T = TypeVar("T")
class TaskThrottle:
"""
A throttle to limit the number of concurrent tasks using semaphores.
Usage:
As a decorator:
>>> throttle = TaskThrottle(max_concurrent=5)
>>> @throttle.limit
... def my_task(data):
... # Process data
... pass
As a context manager:
>>> throttle = TaskThrottle(max_concurrent=5)
>>> with throttle.lease():
... # Protected code block
... pass
"""
def __init__(self, max_concurrent: int) -> None:
"""
Create a throttle with specified concurrency limit.
Args:
max_concurrent: Maximum number of tasks that can run concurrently
"""
if max_concurrent < 1:
raise ValueError("max_concurrent must be at least 1")
self._semaphore: Semaphore = Semaphore(max_concurrent)
self._max_concurrent: int = max_concurrent
def limit(self, func: Callable[P, T]) -> Callable[P, T]:
"""
Decorator to throttle a task function.
"""
@wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
with self.lease():
return func(*args, **kwargs)
return wrapper
@contextmanager
def lease(self) -> Generator[None, None, None]:
"""
Context manager that acquires/releases a throttle slot.
"""
self._semaphore.acquire()
try:
yield
finally:
self._semaphore.release()
@property
def max_concurrent(self) -> int:
"""Get the configured concurrency limit."""
return self._max_concurrent