mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-30 17:41:54 +08:00
simplified AioNotifier()
This commit is contained in:
@@ -218,7 +218,7 @@ class MsdFileReader(BaseMsdReader): # pylint: disable=too-many-instance-attribu
|
||||
now = time.monotonic()
|
||||
if self.__tick + 1 < now or self.__readed == self.__file_size:
|
||||
self.__tick = now
|
||||
await self.__notifier.notify()
|
||||
self.__notifier.notify()
|
||||
|
||||
yield chunk
|
||||
|
||||
@@ -277,7 +277,7 @@ class MsdFileWriter(BaseMsdWriter): # pylint: disable=too-many-instance-attribu
|
||||
now = time.monotonic()
|
||||
if self.__tick + 1 < now:
|
||||
self.__tick = now
|
||||
await self.__notifier.notify()
|
||||
self.__notifier.notify()
|
||||
|
||||
return self.__written
|
||||
|
||||
|
||||
@@ -121,13 +121,13 @@ class _State:
|
||||
async def busy(self, check_online: bool=True) -> AsyncGenerator[None, None]:
|
||||
async with self._region:
|
||||
async with self._lock:
|
||||
await self.__notifier.notify()
|
||||
self.__notifier.notify()
|
||||
if check_online:
|
||||
if self.vd is None:
|
||||
raise MsdOfflineError()
|
||||
assert self.storage
|
||||
yield
|
||||
await self.__notifier.notify()
|
||||
self.__notifier.notify()
|
||||
|
||||
def is_busy(self) -> bool:
|
||||
return self._region.is_busy()
|
||||
@@ -173,7 +173,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
|
||||
logger = get_logger(0)
|
||||
logger.info("Using OTG gadget %r as MSD", gadget)
|
||||
aiotools.run_sync(self.__reload_state())
|
||||
aiotools.run_sync(self.__reload_state(notify=False))
|
||||
|
||||
@classmethod
|
||||
def get_plugin_options(cls) -> Dict:
|
||||
@@ -334,7 +334,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
async with self.__state._region: # pylint: disable=protected-access
|
||||
try:
|
||||
async with self.__state._lock: # pylint: disable=protected-access
|
||||
await self.__notifier.notify()
|
||||
self.__notifier.notify()
|
||||
assert self.__state.storage
|
||||
assert self.__state.vd
|
||||
|
||||
@@ -361,10 +361,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
finally:
|
||||
try:
|
||||
await asyncio.shield(self.__notifier.notify())
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
self.__notifier.notify()
|
||||
|
||||
@contextlib.asynccontextmanager
|
||||
async def write_image(self, name: str, size: int, remove_incomplete: Optional[bool]) -> AsyncGenerator[MsdFileWriter, None]:
|
||||
@@ -373,7 +370,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
path: str = ""
|
||||
try:
|
||||
async with self.__state._lock: # pylint: disable=protected-access
|
||||
await self.__notifier.notify()
|
||||
self.__notifier.notify()
|
||||
assert self.__state.storage
|
||||
assert self.__state.vd
|
||||
|
||||
@@ -395,7 +392,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
chunk_size=self.__write_chunk_size,
|
||||
).open()
|
||||
|
||||
await self.__notifier.notify()
|
||||
self.__notifier.notify()
|
||||
yield self.__writer
|
||||
self.__set_image_complete(name, self.__writer.is_complete())
|
||||
|
||||
@@ -421,10 +418,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
await asyncio.shield(self.__reload_state())
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
try:
|
||||
await asyncio.shield(self.__notifier.notify())
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
@aiotools.atomic
|
||||
async def remove(self, name: str) -> None:
|
||||
@@ -470,7 +463,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
while True:
|
||||
# Активно ждем, пока не будут на месте все каталоги.
|
||||
await self.__reload_state()
|
||||
await self.__notifier.notify()
|
||||
if self.__state.vd:
|
||||
break
|
||||
await asyncio.sleep(5)
|
||||
@@ -483,7 +475,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
|
||||
# После установки вотчеров еще раз проверяем стейт, чтобы ничего не потерять
|
||||
await self.__reload_state()
|
||||
await self.__notifier.notify()
|
||||
|
||||
while self.__state.vd: # Если живы после предыдущей проверки
|
||||
need_restart = False
|
||||
@@ -499,12 +490,11 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
break
|
||||
if need_reload_state:
|
||||
await self.__reload_state()
|
||||
await self.__notifier.notify()
|
||||
except Exception:
|
||||
logger.exception("Unexpected MSD watcher error")
|
||||
time.sleep(1)
|
||||
|
||||
async def __reload_state(self) -> None:
|
||||
async def __reload_state(self, notify: bool=True) -> None:
|
||||
logger = get_logger(0)
|
||||
async with self.__state._lock: # pylint: disable=protected-access
|
||||
try:
|
||||
@@ -541,6 +531,8 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
self.__state.vd.image = None
|
||||
|
||||
self.__state.vd.connected = False
|
||||
if notify:
|
||||
self.__notifier.notify()
|
||||
|
||||
async def __setup_initial(self) -> None:
|
||||
if self.__initial_image:
|
||||
|
||||
@@ -245,7 +245,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
||||
).open()
|
||||
|
||||
await self.__write_image_info(False)
|
||||
await self.__notifier.notify()
|
||||
self.__notifier.notify()
|
||||
yield self.__device_writer
|
||||
await self.__write_image_info(True)
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user