AioExclusiveRegion API is sync now

This commit is contained in:
Maxim Devaev 2024-10-26 15:51:33 +03:00
parent 399712c684
commit a84242c9bc
5 changed files with 23 additions and 23 deletions

View File

@ -297,7 +297,7 @@ class AioExclusiveRegion:
def is_busy(self) -> bool: def is_busy(self) -> bool:
return self.__busy return self.__busy
async def enter(self) -> None: def enter(self) -> None:
if not self.__busy: if not self.__busy:
self.__busy = True self.__busy = True
try: try:
@ -309,22 +309,22 @@ class AioExclusiveRegion:
return return
raise self.__exc_type() raise self.__exc_type()
async def exit(self) -> None: def exit(self) -> None:
self.__busy = False self.__busy = False
if self.__notifier: if self.__notifier:
self.__notifier.notify() self.__notifier.notify()
async def __aenter__(self) -> None: def __enter__(self) -> None:
await self.enter() self.enter()
async def __aexit__( def __exit__(
self, self,
_exc_type: type[BaseException], _exc_type: type[BaseException],
_exc: BaseException, _exc: BaseException,
_tb: types.TracebackType, _tb: types.TracebackType,
) -> None: ) -> None:
await self.exit() self.exit()
async def run_region_task( async def run_region_task(
@ -339,7 +339,7 @@ async def run_region_task(
async def wrapper() -> None: async def wrapper() -> None:
try: try:
async with region: with region:
entered.set_result(None) entered.set_result(None)
await func(*args, **kwargs) await func(*args, **kwargs)
except region.get_exc_type(): except region.get_exc_type():

View File

@ -185,7 +185,7 @@ class _GpioOutput: # pylint: disable=too-many-instance-attributes
@aiotools.atomic_fg @aiotools.atomic_fg
async def __run_action(self, wait: bool, name: str, func: Callable, *args: Any) -> None: async def __run_action(self, wait: bool, name: str, func: Callable, *args: Any) -> None:
if wait: if wait:
async with self.__region: with self.__region:
await func(*args) await func(*args)
else: else:
await aiotools.run_region_task( await aiotools.run_region_task(

View File

@ -191,7 +191,7 @@ class Plugin(BaseAtx): # pylint: disable=too-many-instance-attributes
@aiotools.atomic_fg @aiotools.atomic_fg
async def __click(self, name: str, pin: int, delay: float, wait: bool) -> None: async def __click(self, name: str, pin: int, delay: float, wait: bool) -> None:
if wait: if wait:
async with self.__region: with self.__region:
await self.__inner_click(name, pin, delay) await self.__inner_click(name, pin, delay)
else: else:
await aiotools.run_region_task( await aiotools.run_region_task(

View File

@ -96,7 +96,7 @@ class _State:
@contextlib.asynccontextmanager @contextlib.asynccontextmanager
async def busy(self, check_online: bool=True) -> AsyncGenerator[None, None]: async def busy(self, check_online: bool=True) -> AsyncGenerator[None, None]:
async with self._region: with self._region:
async with self._lock: async with self._lock:
self.__notifier.notify() self.__notifier.notify()
if check_online: if check_online:
@ -292,7 +292,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
@contextlib.asynccontextmanager @contextlib.asynccontextmanager
async def read_image(self, name: str) -> AsyncGenerator[MsdFileReader, None]: async def read_image(self, name: str) -> AsyncGenerator[MsdFileReader, None]:
try: try:
async with self.__state._region: # pylint: disable=protected-access with self.__state._region: # pylint: disable=protected-access
try: try:
async with self.__state._lock: # pylint: disable=protected-access async with self.__state._lock: # pylint: disable=protected-access
self.__notifier.notify() self.__notifier.notify()
@ -313,7 +313,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
@contextlib.asynccontextmanager @contextlib.asynccontextmanager
async def write_image(self, name: str, size: int, remove_incomplete: (bool | None)) -> AsyncGenerator[MsdFileWriter, None]: async def write_image(self, name: str, size: int, remove_incomplete: (bool | None)) -> AsyncGenerator[MsdFileWriter, None]:
try: try:
async with self.__state._region: # pylint: disable=protected-access with self.__state._region: # pylint: disable=protected-access
image: (Image | None) = None image: (Image | None) = None
try: try:
async with self.__state._lock: # pylint: disable=protected-access async with self.__state._lock: # pylint: disable=protected-access

View File

@ -40,14 +40,14 @@ async def test_ok__region__access_one() -> None:
async def func() -> None: async def func() -> None:
assert not region.is_busy() assert not region.is_busy()
async with region: with region:
assert region.is_busy() assert region.is_busy()
assert not region.is_busy() assert not region.is_busy()
await func() await func()
assert not region.is_busy() assert not region.is_busy()
await region.exit() region.exit()
assert not region.is_busy() assert not region.is_busy()
@ -57,16 +57,16 @@ async def test_fail__region__access_one() -> None:
async def func() -> None: async def func() -> None:
assert not region.is_busy() assert not region.is_busy()
async with region: with region:
assert region.is_busy() assert region.is_busy()
await region.enter() region.enter()
assert not region.is_busy() assert not region.is_busy()
with pytest.raises(RegionIsBusyError): with pytest.raises(RegionIsBusyError):
await func() await func()
assert not region.is_busy() assert not region.is_busy()
await region.exit() region.exit()
assert not region.is_busy() assert not region.is_busy()
@ -76,21 +76,21 @@ async def test_ok__region__access_two() -> None:
region = AioExclusiveRegion(RegionIsBusyError) region = AioExclusiveRegion(RegionIsBusyError)
async def func1() -> None: async def func1() -> None:
async with region: with region:
await asyncio.sleep(1) await asyncio.sleep(1)
print("done func1()") print("done func1()")
async def func2() -> None: async def func2() -> None:
await asyncio.sleep(2) await asyncio.sleep(2)
print("waiking up func2()") print("waiking up func2()")
async with region: with region:
await asyncio.sleep(1) await asyncio.sleep(1)
print("done func2()") print("done func2()")
await asyncio.gather(func1(), func2()) await asyncio.gather(func1(), func2())
assert not region.is_busy() assert not region.is_busy()
await region.exit() region.exit()
assert not region.is_busy() assert not region.is_busy()
@ -99,13 +99,13 @@ async def test_fail__region__access_two() -> None:
region = AioExclusiveRegion(RegionIsBusyError) region = AioExclusiveRegion(RegionIsBusyError)
async def func1() -> None: async def func1() -> None:
async with region: with region:
await asyncio.sleep(2) await asyncio.sleep(2)
print("done func1()") print("done func1()")
async def func2() -> None: async def func2() -> None:
await asyncio.sleep(1) await asyncio.sleep(1)
async with region: with region:
await asyncio.sleep(1) await asyncio.sleep(1)
print("done func2()") print("done func2()")
@ -114,7 +114,7 @@ async def test_fail__region__access_two() -> None:
assert type(results[1]) is RegionIsBusyError # pylint: disable=unidiomatic-typecheck assert type(results[1]) is RegionIsBusyError # pylint: disable=unidiomatic-typecheck
assert not region.is_busy() assert not region.is_busy()
await region.exit() region.exit()
assert not region.is_busy() assert not region.is_busy()