forked from p15670423/monkey
Agent: Make request_cache() decorator thread-safe
This commit is contained in:
parent
4005ea2924
commit
e2d116fdf1
|
@ -1,3 +1,4 @@
|
||||||
|
import threading
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
from .timer import Timer
|
from .timer import Timer
|
||||||
|
@ -29,6 +30,7 @@ def request_cache(ttl: float):
|
||||||
def decorator(fn):
|
def decorator(fn):
|
||||||
@wraps(fn)
|
@wraps(fn)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
|
with wrapper.lock:
|
||||||
if wrapper.timer.is_expired():
|
if wrapper.timer.is_expired():
|
||||||
wrapper.cached_value = fn(*args, **kwargs)
|
wrapper.cached_value = fn(*args, **kwargs)
|
||||||
wrapper.timer.set(ttl)
|
wrapper.timer.set(ttl)
|
||||||
|
@ -37,6 +39,7 @@ def request_cache(ttl: float):
|
||||||
|
|
||||||
wrapper.cached_value = None
|
wrapper.cached_value = None
|
||||||
wrapper.timer = Timer()
|
wrapper.timer = Timer()
|
||||||
|
wrapper.lock = threading.Lock()
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue