changed region methods to async

This commit is contained in:
Devaev Maxim 2020-03-02 01:26:43 +03:00
parent e855976f05
commit 8972357dbc
5 changed files with 31 additions and 32 deletions

View File

@ -32,7 +32,6 @@ import typing
from typing import List from typing import List
from typing import Callable from typing import Callable
from typing import Coroutine from typing import Coroutine
from typing import Generator
from typing import AsyncGenerator from typing import AsyncGenerator
from typing import Type from typing import Type
from typing import TypeVar from typing import TypeVar
@ -136,34 +135,34 @@ class AioExclusiveRegion:
def is_busy(self) -> bool: def is_busy(self) -> bool:
return self.__busy return self.__busy
def enter(self) -> None: async def enter(self) -> None:
if not self.__busy: if not self.__busy:
self.__busy = True self.__busy = True
return return
raise self.__exc_type() raise self.__exc_type()
def exit(self) -> None: async def exit(self) -> None:
self.__busy = False self.__busy = False
@contextlib.contextmanager @contextlib.asynccontextmanager
def exit_only_on_exception(self) -> Generator[None, None, None]: async def exit_only_on_exception(self) -> AsyncGenerator[None, None]:
self.enter() await self.enter()
try: try:
yield yield
except: # noqa: E722 except: # noqa: E722
self.exit() await self.exit()
raise raise
def __enter__(self) -> None: async def __aenter__(self) -> None:
self.enter() await self.enter()
def __exit__( async def __aexit__(
self, self,
_exc_type: Type[BaseException], _exc_type: Type[BaseException],
_exc: BaseException, _exc: BaseException,
_tb: types.TracebackType, _tb: types.TracebackType,
) -> None: ) -> None:
self.exit() await self.exit()
# ===== # =====

View File

@ -162,7 +162,7 @@ class Plugin(BaseAtx): # pylint: disable=too-many-instance-attributes
@aiotools.atomic @aiotools.atomic
async def __click(self, name: str, pin: int, delay: float) -> None: async def __click(self, name: str, pin: int, delay: float) -> None:
with self.__region.exit_only_on_exception(): async with self.__region.exit_only_on_exception():
await self.__inner_click(name, pin, delay) await self.__inner_click(name, pin, delay)
@aiotools.tasked @aiotools.tasked
@ -176,5 +176,5 @@ class Plugin(BaseAtx): # pylint: disable=too-many-instance-attributes
gpio.write(pin, False) gpio.write(pin, False)
await asyncio.sleep(1) await asyncio.sleep(1)
finally: finally:
self.__region.exit() await self.__region.exit()
get_logger(0).info("Clicked ATX button %r", name) get_logger(0).info("Clicked ATX button %r", name)

View File

@ -114,7 +114,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]:
with self._region: async with self._region:
async with self._lock: async with self._lock:
await self.__notifier.notify() await self.__notifier.notify()
if check_online: if check_online:
@ -304,7 +304,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
@contextlib.asynccontextmanager @contextlib.asynccontextmanager
async def write_image(self, name: str) -> AsyncGenerator[None, None]: async def write_image(self, name: str) -> AsyncGenerator[None, None]:
try: try:
with self.__state._region: # pylint: disable=protected-access async 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
await self.__state_notifier.notify() await self.__state_notifier.notify()

View File

@ -234,7 +234,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
@aiotools.atomic @aiotools.atomic
async def reset(self) -> None: async def reset(self) -> None:
with self.__region.exit_only_on_exception(): async with self.__region.exit_only_on_exception():
await self.__inner_reset() await self.__inner_reset()
@aiotools.tasked @aiotools.tasked
@ -254,7 +254,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
try: try:
gpio.write(self.__reset_pin, False) gpio.write(self.__reset_pin, False)
finally: finally:
self.__region.exit() await self.__region.exit()
await self.__state_queue.put(await self.get_state()) await self.__state_queue.put(await self.get_state())
@aiotools.atomic @aiotools.atomic
@ -277,7 +277,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
async with self.__working(): async with self.__working():
notify = False notify = False
try: try:
with self.__region: async with self.__region:
if self.__connected: if self.__connected:
raise MsdConnectedError() raise MsdConnectedError()
notify = True notify = True
@ -294,7 +294,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
async with self.__working(): async with self.__working():
notify = False notify = False
try: try:
with self.__region: async with self.__region:
if not self.__connected: if not self.__connected:
raise MsdDisconnectedError() raise MsdDisconnectedError()
notify = True notify = True
@ -315,7 +315,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
@contextlib.asynccontextmanager @contextlib.asynccontextmanager
async def write_image(self, name: str) -> AsyncGenerator[None, None]: async def write_image(self, name: str) -> AsyncGenerator[None, None]:
async with self.__working(): async with self.__working():
self.__region.enter() await self.__region.enter()
try: try:
assert self.__device_info assert self.__device_info
if self.__connected: if self.__connected:
@ -333,7 +333,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
await self.__close_device_file() await self.__close_device_file()
await self.__load_device_info() await self.__load_device_info()
finally: finally:
self.__region.exit() await self.__region.exit()
await self.__state_queue.put(await self.get_state()) await self.__state_queue.put(await self.get_state())
async def write_image_chunk(self, chunk: bytes) -> int: async def write_image_chunk(self, chunk: bytes) -> int:

View File

@ -39,14 +39,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()
with region: async 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()
region.exit() await region.exit()
assert not region.is_busy() assert not region.is_busy()
@ -56,16 +56,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()
with region: async with region:
assert region.is_busy() assert region.is_busy()
region.enter() await 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()
region.exit() await region.exit()
assert not region.is_busy() assert not region.is_busy()
@ -75,21 +75,21 @@ async def test_ok__region__access_two() -> None:
region = AioExclusiveRegion(RegionIsBusyError) region = AioExclusiveRegion(RegionIsBusyError)
async def func1() -> None: async def func1() -> None:
with region: async 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()")
with region: async 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()
region.exit() await region.exit()
assert not region.is_busy() assert not region.is_busy()
@ -98,13 +98,13 @@ async def test_fail__region__access_two() -> None:
region = AioExclusiveRegion(RegionIsBusyError) region = AioExclusiveRegion(RegionIsBusyError)
async def func1() -> None: async def func1() -> None:
with region: async 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)
with region: async with region:
await asyncio.sleep(1) await asyncio.sleep(1)
print("done func2()") print("done func2()")
@ -113,5 +113,5 @@ async def test_fail__region__access_two() -> None:
assert type(results[1]) == RegionIsBusyError # pylint: disable=unidiomatic-typecheck assert type(results[1]) == RegionIsBusyError # pylint: disable=unidiomatic-typecheck
assert not region.is_busy() assert not region.is_busy()
region.exit() await region.exit()
assert not region.is_busy() assert not region.is_busy()