Skip to content

Commit 1a7ba4f

Browse files
authored
Added acquire_timeout context manager
1 parent 34cb6a7 commit 1a7ba4f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/labthings/core/lock.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from gevent.hub import getcurrent
22
from gevent.lock import RLock as _RLock
33

4+
from contextlib import contextmanager
45
import logging
56

67
from .exceptions import LockError
@@ -31,6 +32,13 @@ def __init__(self, timeout=None, name=None):
3132
self.timeout = timeout
3233
self.name = name
3334

35+
@contextmanager
36+
def acquire_timeout(self, timeout, blocking=True):
37+
result = self.acquire(timeout=timeout, blocking=blocking)
38+
yield result
39+
if result:
40+
self.release()
41+
3442
def locked(self):
3543
return self._lock.locked()
3644

@@ -82,6 +90,13 @@ def __init__(self, locks, timeout=None):
8290
self.locks = locks
8391
self.timeout = timeout
8492

93+
@contextmanager
94+
def acquire_timeout(self, timeout, blocking=True):
95+
result = self.acquire(timeout=timeout, blocking=blocking)
96+
yield result
97+
if result:
98+
self.release()
99+
85100
def acquire(self, blocking=True, timeout=sentinel):
86101
if timeout is sentinel:
87102
timeout = self.timeout

0 commit comments

Comments
 (0)