Skip to content

Commit 455028b

Browse files
committed
wrap RLock in asynccontextmanager
1 parent ea08803 commit 455028b

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

robotcode/utils/async_tools.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import weakref
1111
from collections import deque
1212
from concurrent.futures.thread import ThreadPoolExecutor
13+
from contextlib import asynccontextmanager
1314
from types import TracebackType
1415
from typing import (
1516
Any,
@@ -574,7 +575,7 @@ async def __aenter__(self) -> None:
574575
async def __aexit__(
575576
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
576577
) -> None:
577-
self.release()
578+
await self.release()
578579

579580
def __repr__(self) -> str:
580581
res = super().__repr__()
@@ -583,19 +584,22 @@ def __repr__(self) -> str:
583584
extra = f"{extra}, waiters:{len(self._waiters)}"
584585
return f"<{res[1:-1]} [{extra}]>"
585586

586-
def locked(self) -> bool:
587-
return self._locked
587+
@asynccontextmanager
588+
async def __inner_lock(self) -> AsyncGenerator[bool, None]:
589+
with self._lock as r:
590+
yield r
588591

589592
async def acquire(self) -> bool:
590-
591-
with self._lock:
593+
async with self.__inner_lock():
592594
if not self._locked and (self._waiters is None or all(w.cancelled() for w in self._waiters)):
593595
self._locked = True
594596
return True
595597

596598
if self._waiters is None:
597599
self._waiters = deque()
600+
598601
fut = create_sub_future()
602+
599603
with self._lock:
600604
self._waiters.append(fut)
601605

@@ -609,14 +613,14 @@ async def acquire(self) -> bool:
609613
self._wake_up_first()
610614
raise
611615

612-
with self._lock:
616+
async with self.__inner_lock():
613617
self._locked = True
614618

615619
return True
616620

617-
def release(self) -> None:
621+
async def release(self) -> None:
618622
if self._locked:
619-
with self._lock:
623+
async with self.__inner_lock():
620624
self._locked = False
621625
self._wake_up_first()
622626
else:

0 commit comments

Comments
 (0)