diff options
| author | Scott <[email protected]> | 2022-01-30 17:37:58 -0800 |
|---|---|---|
| committer | Scott <[email protected]> | 2022-01-30 17:37:58 -0800 |
| commit | 5e09a33068fcdf6d43f12477dd943e108e11ae06 (patch) | |
| tree | 3f6e020b39c4c58c557ed3ae1409947a9be8b56f /decorator_utils.py | |
| parent | 5d04ec30aabad127c4eb9494a7e1be72847612e3 (diff) | |
Improve the locking decorators; @synchronized(lock).
Diffstat (limited to 'decorator_utils.py')
| -rw-r--r-- | decorator_utils.py | 39 |
1 files changed, 10 insertions, 29 deletions
diff --git a/decorator_utils.py b/decorator_utils.py index 1ecbce3..a956a21 100644 --- a/decorator_utils.py +++ b/decorator_utils.py @@ -704,38 +704,19 @@ def timeout( return decorate -class non_reentrant_code(object): - def __init__(self): - self._lock = threading.RLock - self._entered = False - - def __call__(self, f): - def _gatekeeper(*args, **kwargs): - with self._lock: - if self._entered: - return - self._entered = True - f(*args, **kwargs) - self._entered = False +def synchronized(lock): + def wrap(f): + @functools.wraps(f) + def _gatekeeper(*args, **kw): + lock.acquire() + try: + return f(*args, **kw) + finally: + lock.release() return _gatekeeper - -class rlocked(object): - def __init__(self): - self._lock = threading.RLock - self._entered = False - - def __call__(self, f): - def _gatekeeper(*args, **kwargs): - with self._lock: - if self._entered: - return - self._entered = True - f(*args, **kwargs) - self._entered = False - - return _gatekeeper + return wrap def call_with_sample_rate(sample_rate: float) -> Callable: |
