mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-13 01:30:31 +08:00
using shield_fg() in atomic (now atomic_fg)
This commit is contained in:
parent
fdc3edfa79
commit
e37a7254d5
@ -125,10 +125,10 @@ def shield_fg(aw: Awaitable): # type: ignore
|
|||||||
return outer
|
return outer
|
||||||
|
|
||||||
|
|
||||||
def atomic(func: _FunctionT) -> _FunctionT:
|
def atomic_fg(func: _FunctionT) -> _FunctionT:
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||||
return (await asyncio.shield(func(*args, **kwargs)))
|
return (await shield_fg(func(*args, **kwargs)))
|
||||||
return typing.cast(_FunctionT, wrapper)
|
return typing.cast(_FunctionT, wrapper)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -126,13 +126,13 @@ class JanusRunner: # pylint: disable=too-many-instance-attributes
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def __start_janus(self, netcfg: _Netcfg) -> None:
|
async def __start_janus(self, netcfg: _Netcfg) -> None:
|
||||||
get_logger(0).info("Starting Janus ...")
|
get_logger(0).info("Starting Janus ...")
|
||||||
assert not self.__janus_task
|
assert not self.__janus_task
|
||||||
self.__janus_task = asyncio.create_task(self.__janus_task_loop(netcfg))
|
self.__janus_task = asyncio.create_task(self.__janus_task_loop(netcfg))
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def __stop_janus(self) -> None:
|
async def __stop_janus(self) -> None:
|
||||||
if self.__janus_task:
|
if self.__janus_task:
|
||||||
get_logger(0).info("Stopping Janus ...")
|
get_logger(0).info("Stopping Janus ...")
|
||||||
|
|||||||
@ -113,7 +113,7 @@ class AuthManager:
|
|||||||
assert self.__enabled
|
assert self.__enabled
|
||||||
return self.__tokens.get(token)
|
return self.__tokens.get(token)
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def cleanup(self) -> None:
|
async def cleanup(self) -> None:
|
||||||
if self.__enabled:
|
if self.__enabled:
|
||||||
assert self.__internal_service
|
assert self.__internal_service
|
||||||
|
|||||||
@ -221,7 +221,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def ensure_start(self, reset: bool) -> None:
|
async def ensure_start(self, reset: bool) -> None:
|
||||||
if not self.__streamer_task or self.__stop_task:
|
if not self.__streamer_task or self.__stop_task:
|
||||||
logger = get_logger(0)
|
logger = get_logger(0)
|
||||||
@ -241,7 +241,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
|||||||
logger.info("Starting streamer ...")
|
logger.info("Starting streamer ...")
|
||||||
await self.__inner_start()
|
await self.__inner_start()
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def ensure_stop(self, immediately: bool) -> None:
|
async def ensure_stop(self, immediately: bool) -> None:
|
||||||
if self.__streamer_task:
|
if self.__streamer_task:
|
||||||
logger = get_logger(0)
|
logger = get_logger(0)
|
||||||
@ -384,7 +384,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def cleanup(self) -> None:
|
async def cleanup(self) -> None:
|
||||||
await self.ensure_stop(immediately=True)
|
await self.ensure_stop(immediately=True)
|
||||||
if self.__http_session:
|
if self.__http_session:
|
||||||
@ -409,12 +409,12 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def __inner_start(self) -> None:
|
async def __inner_start(self) -> None:
|
||||||
assert not self.__streamer_task
|
assert not self.__streamer_task
|
||||||
self.__streamer_task = asyncio.create_task(self.__streamer_task_loop())
|
self.__streamer_task = asyncio.create_task(self.__streamer_task_loop())
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def __inner_stop(self) -> None:
|
async def __inner_stop(self) -> None:
|
||||||
assert self.__streamer_task
|
assert self.__streamer_task
|
||||||
self.__streamer_task.cancel()
|
self.__streamer_task.cancel()
|
||||||
|
|||||||
@ -172,7 +172,7 @@ class _GpioOutput: # pylint: disable=too-many-instance-attributes
|
|||||||
raise GpioSwitchNotSupported()
|
raise GpioSwitchNotSupported()
|
||||||
await self.__run_action(wait, "switch", self.__inner_switch, state)
|
await self.__run_action(wait, "switch", self.__inner_switch, state)
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def pulse(self, delay: float, wait: bool) -> None:
|
async def pulse(self, delay: float, wait: bool) -> None:
|
||||||
if not self.__pulse_delay:
|
if not self.__pulse_delay:
|
||||||
raise GpioPulseNotSupported()
|
raise GpioPulseNotSupported()
|
||||||
@ -181,7 +181,7 @@ class _GpioOutput: # pylint: disable=too-many-instance-attributes
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
|
|
||||||
@aiotools.atomic
|
@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:
|
async with self.__region:
|
||||||
@ -192,20 +192,20 @@ class _GpioOutput: # pylint: disable=too-many-instance-attributes
|
|||||||
self.__region, self.__action_task_wrapper, name, func, *args,
|
self.__region, self.__action_task_wrapper, name, func, *args,
|
||||||
)
|
)
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def __action_task_wrapper(self, name: str, func: Callable, *args: Any) -> None:
|
async def __action_task_wrapper(self, name: str, func: Callable, *args: Any) -> None:
|
||||||
try:
|
try:
|
||||||
return (await func(*args))
|
return (await func(*args))
|
||||||
except GpioDriverOfflineError:
|
except GpioDriverOfflineError:
|
||||||
get_logger(0).error("Can't perform %s of %s or operation was not completed: driver offline", name, self)
|
get_logger(0).error("Can't perform %s of %s or operation was not completed: driver offline", name, self)
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def __inner_switch(self, state: bool) -> None:
|
async def __inner_switch(self, state: bool) -> None:
|
||||||
await self.__write(state)
|
await self.__write(state)
|
||||||
get_logger(0).info("Ensured switch %s to state=%d", self, state)
|
get_logger(0).info("Ensured switch %s to state=%d", self, state)
|
||||||
await asyncio.sleep(self.__busy_delay)
|
await asyncio.sleep(self.__busy_delay)
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def __inner_pulse(self, delay: float) -> None:
|
async def __inner_pulse(self, delay: float) -> None:
|
||||||
try:
|
try:
|
||||||
await self.__write(True)
|
await self.__write(True)
|
||||||
|
|||||||
@ -185,7 +185,7 @@ class Plugin(BaseAtx): # pylint: disable=too-many-instance-attributes
|
|||||||
async def __get_power(self) -> bool:
|
async def __get_power(self) -> bool:
|
||||||
return (await self.get_state())["leds"]["power"]
|
return (await self.get_state())["leds"]["power"]
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def __click(self, name: str, line: gpiod.Line, delay: float, wait: bool) -> None:
|
async def __click(self, name: str, line: gpiod.Line, delay: float, wait: bool) -> None:
|
||||||
if wait:
|
if wait:
|
||||||
async with self.__region:
|
async with self.__region:
|
||||||
@ -196,7 +196,7 @@ class Plugin(BaseAtx): # pylint: disable=too-many-instance-attributes
|
|||||||
self.__region, self.__inner_click, name, line, delay,
|
self.__region, self.__inner_click, name, line, delay,
|
||||||
)
|
)
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def __inner_click(self, name: str, line: gpiod.Line, delay: float) -> None:
|
async def __inner_click(self, name: str, line: gpiod.Line, delay: float) -> None:
|
||||||
await aiogp.pulse(line, delay, 1)
|
await aiogp.pulse(line, delay, 1)
|
||||||
get_logger(0).info("Clicked ATX button %r", name)
|
get_logger(0).info("Clicked ATX button %r", name)
|
||||||
|
|||||||
@ -234,7 +234,7 @@ class BaseMcuHid(BaseHid, multiprocessing.Process): # pylint: disable=too-many-
|
|||||||
async def reset(self) -> None:
|
async def reset(self) -> None:
|
||||||
self.__reset_required_event.set()
|
self.__reset_required_event.set()
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def cleanup(self) -> None:
|
async def cleanup(self) -> None:
|
||||||
if self.is_alive():
|
if self.is_alive():
|
||||||
get_logger(0).info("Stopping HID daemon ...")
|
get_logger(0).info("Stopping HID daemon ...")
|
||||||
|
|||||||
@ -164,7 +164,7 @@ class Plugin(BaseHid): # pylint: disable=too-many-instance-attributes
|
|||||||
self.clear_events()
|
self.clear_events()
|
||||||
self.__server.queue_event(ResetEvent())
|
self.__server.queue_event(ResetEvent())
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def cleanup(self) -> None:
|
async def cleanup(self) -> None:
|
||||||
if self.__proc is not None:
|
if self.__proc is not None:
|
||||||
if self.__proc.is_alive():
|
if self.__proc.is_alive():
|
||||||
|
|||||||
@ -246,7 +246,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
async def systask(self) -> None:
|
async def systask(self) -> None:
|
||||||
await self.__watch_inotify()
|
await self.__watch_inotify()
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def reset(self) -> None:
|
async def reset(self) -> None:
|
||||||
async with self.__state.busy(check_online=False):
|
async with self.__state.busy(check_online=False):
|
||||||
try:
|
try:
|
||||||
@ -257,14 +257,14 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
except Exception:
|
except Exception:
|
||||||
get_logger(0).exception("Can't reset MSD properly")
|
get_logger(0).exception("Can't reset MSD properly")
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def cleanup(self) -> None:
|
async def cleanup(self) -> None:
|
||||||
await self.__close_reader()
|
await self.__close_reader()
|
||||||
await self.__close_writer()
|
await self.__close_writer()
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def set_params(
|
async def set_params(
|
||||||
self,
|
self,
|
||||||
name: Optional[str]=None,
|
name: Optional[str]=None,
|
||||||
@ -299,7 +299,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
if rw:
|
if rw:
|
||||||
self.__state.vd.cdrom = False
|
self.__state.vd.cdrom = False
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def set_connected(self, connected: bool) -> None:
|
async def set_connected(self, connected: bool) -> None:
|
||||||
async with self.__state.busy():
|
async with self.__state.busy():
|
||||||
assert self.__state.vd
|
assert self.__state.vd
|
||||||
@ -407,7 +407,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
# так что форсим обновление вручную, чтобы получить актуальное состояние.
|
# так что форсим обновление вручную, чтобы получить актуальное состояние.
|
||||||
await aiotools.shield_fg(self.__reload_state())
|
await aiotools.shield_fg(self.__reload_state())
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def remove(self, name: str) -> None:
|
async def remove(self, name: str) -> None:
|
||||||
async with self.__state.busy():
|
async with self.__state.busy():
|
||||||
assert self.__state.storage
|
assert self.__state.storage
|
||||||
|
|||||||
@ -156,14 +156,14 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
prev_state = state
|
prev_state = state
|
||||||
await self.__notifier.wait()
|
await self.__notifier.wait()
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def reset(self) -> None:
|
async def reset(self) -> None:
|
||||||
await aiotools.run_region_task(
|
await aiotools.run_region_task(
|
||||||
"Can't reset MSD or operation was not completed",
|
"Can't reset MSD or operation was not completed",
|
||||||
self.__region, self.__inner_reset,
|
self.__region, self.__inner_reset,
|
||||||
)
|
)
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def __inner_reset(self) -> None:
|
async def __inner_reset(self) -> None:
|
||||||
await self.__gpio.reset()
|
await self.__gpio.reset()
|
||||||
self.__gpio.switch_to_local()
|
self.__gpio.switch_to_local()
|
||||||
@ -171,7 +171,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
await self.__load_device_info()
|
await self.__load_device_info()
|
||||||
get_logger(0).info("MSD reset has been successful")
|
get_logger(0).info("MSD reset has been successful")
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def cleanup(self) -> None:
|
async def cleanup(self) -> None:
|
||||||
try:
|
try:
|
||||||
await self.__close_device_writer()
|
await self.__close_device_writer()
|
||||||
@ -180,7 +180,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def set_params(
|
async def set_params(
|
||||||
self,
|
self,
|
||||||
name: Optional[str]=None,
|
name: Optional[str]=None,
|
||||||
@ -196,7 +196,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
if rw is not None:
|
if rw is not None:
|
||||||
raise MsdRwNotSupported()
|
raise MsdRwNotSupported()
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def set_connected(self, connected: bool) -> None:
|
async def set_connected(self, connected: bool) -> None:
|
||||||
async with self.__working():
|
async with self.__working():
|
||||||
async with self.__region:
|
async with self.__region:
|
||||||
@ -254,7 +254,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
finally:
|
finally:
|
||||||
await aiotools.shield_fg(self.__load_device_info())
|
await aiotools.shield_fg(self.__load_device_info())
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic_fg
|
||||||
async def remove(self, name: str) -> None:
|
async def remove(self, name: str) -> None:
|
||||||
async with self.__working():
|
async with self.__working():
|
||||||
raise MsdMultiNotSupported()
|
raise MsdMultiNotSupported()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user