mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-31 18:11:54 +08:00
refactoring
This commit is contained in:
@@ -37,14 +37,14 @@ _AtomicF = TypeVar("_AtomicF", bound=Callable[..., Any])
|
|||||||
|
|
||||||
def atomic(method: _AtomicF) -> _AtomicF:
|
def atomic(method: _AtomicF) -> _AtomicF:
|
||||||
@functools.wraps(method)
|
@functools.wraps(method)
|
||||||
async def wrapper(*args: object, **kwargs: object) -> Any:
|
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||||
return (await asyncio.shield(method(*args, **kwargs)))
|
return (await asyncio.shield(method(*args, **kwargs)))
|
||||||
return typing.cast(_AtomicF, wrapper)
|
return typing.cast(_AtomicF, wrapper)
|
||||||
|
|
||||||
|
|
||||||
def tasked(method: Callable[..., Any]) -> Callable[..., asyncio.Task]:
|
def tasked(method: Callable[..., Any]) -> Callable[..., asyncio.Task]:
|
||||||
@functools.wraps(method)
|
@functools.wraps(method)
|
||||||
async def wrapper(*args: object, **kwargs: object) -> asyncio.Task:
|
async def wrapper(*args: Any, **kwargs: Any) -> asyncio.Task:
|
||||||
return create_short_task(method(*args, **kwargs))
|
return create_short_task(method(*args, **kwargs))
|
||||||
return typing.cast(Callable[..., asyncio.Task], wrapper)
|
return typing.cast(Callable[..., asyncio.Task], wrapper)
|
||||||
|
|
||||||
@@ -64,3 +64,14 @@ async def gather_short_tasks() -> None:
|
|||||||
for task in asyncio.Task.all_tasks()
|
for task in asyncio.Task.all_tasks()
|
||||||
if getattr(task, _ATTR_SHORT_TASK, False)
|
if getattr(task, _ATTR_SHORT_TASK, False)
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
|
_RetvalT = TypeVar("_RetvalT")
|
||||||
|
|
||||||
|
|
||||||
|
async def run_async(method: Callable[..., _RetvalT], *args: Any) -> _RetvalT:
|
||||||
|
return (await asyncio.get_running_loop().run_in_executor(None, method, *args))
|
||||||
|
|
||||||
|
|
||||||
|
def run_sync(coro: Coroutine[Any, Any, _RetvalT]) -> _RetvalT:
|
||||||
|
return asyncio.get_event_loop().run_until_complete(coro)
|
||||||
|
|||||||
@@ -26,13 +26,13 @@ from typing import List
|
|||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from ... import aiotools
|
|
||||||
|
|
||||||
from ...logging import get_logger
|
from ...logging import get_logger
|
||||||
|
|
||||||
from ...plugins.auth import BaseAuthService
|
from ...plugins.auth import BaseAuthService
|
||||||
from ...plugins.auth import get_auth_service_class
|
from ...plugins.auth import get_auth_service_class
|
||||||
|
|
||||||
|
from ... import aiotools
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
class AuthManager:
|
class AuthManager:
|
||||||
|
|||||||
@@ -21,7 +21,6 @@
|
|||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import asyncio
|
|
||||||
import contextlib
|
import contextlib
|
||||||
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
@@ -33,6 +32,8 @@ from ...logging import get_logger
|
|||||||
|
|
||||||
from ...yamlconf.loader import load_yaml_file
|
from ...yamlconf.loader import load_yaml_file
|
||||||
|
|
||||||
|
from ... import aiotools
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
class InfoManager:
|
class InfoManager:
|
||||||
@@ -46,10 +47,10 @@ class InfoManager:
|
|||||||
self.__extras_path = extras_path
|
self.__extras_path = extras_path
|
||||||
|
|
||||||
async def get_meta(self) -> Dict:
|
async def get_meta(self) -> Dict:
|
||||||
return (await asyncio.get_running_loop().run_in_executor(None, load_yaml_file, self.__meta_path))
|
return (await aiotools.run_async(load_yaml_file, self.__meta_path))
|
||||||
|
|
||||||
async def get_extras(self) -> Dict:
|
async def get_extras(self) -> Dict:
|
||||||
return (await asyncio.get_running_loop().run_in_executor(None, self.__inner_get_extras))
|
return (await aiotools.run_async(self.__inner_get_extras))
|
||||||
|
|
||||||
def __inner_get_extras(self) -> Dict:
|
def __inner_get_extras(self) -> Dict:
|
||||||
extras: Dict[str, Dict] = {}
|
extras: Dict[str, Dict] = {}
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ import aiofiles.base
|
|||||||
|
|
||||||
from ...logging import get_logger
|
from ...logging import get_logger
|
||||||
|
|
||||||
from ... import aioregion
|
|
||||||
from ... import aiotools
|
from ... import aiotools
|
||||||
|
from ... import aioregion
|
||||||
from ... import gpio
|
from ... import gpio
|
||||||
|
|
||||||
|
|
||||||
@@ -245,7 +245,7 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
|||||||
logger.info("Using %r as mass-storage device", self._device_path)
|
logger.info("Using %r as mass-storage device", self._device_path)
|
||||||
try:
|
try:
|
||||||
logger.info("Enabled image metadata writing")
|
logger.info("Enabled image metadata writing")
|
||||||
asyncio.get_event_loop().run_until_complete(self.connect_to_kvm(initial=True))
|
aiotools.run_sync(self.connect_to_kvm(initial=True))
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
if isinstance(err, MsdError):
|
if isinstance(err, MsdError):
|
||||||
log = logger.error
|
log = logger.error
|
||||||
@@ -374,10 +374,10 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
|||||||
assert self.__device_file
|
assert self.__device_file
|
||||||
await self.__device_file.write(data)
|
await self.__device_file.write(data)
|
||||||
await self.__device_file.flush()
|
await self.__device_file.flush()
|
||||||
await asyncio.get_running_loop().run_in_executor(None, os.fsync, self.__device_file.fileno())
|
await aiotools.run_async(os.fsync, self.__device_file.fileno())
|
||||||
|
|
||||||
async def __load_device_info(self) -> None:
|
async def __load_device_info(self) -> None:
|
||||||
device_info = await asyncio.get_running_loop().run_in_executor(None, _explore_device, self._device_path)
|
device_info = await aiotools.run_async(_explore_device, self._device_path)
|
||||||
if not device_info:
|
if not device_info:
|
||||||
raise MsdError("Can't explore device %r" % (self._device_path))
|
raise MsdError("Can't explore device %r" % (self._device_path))
|
||||||
self.__device_info = self.__saved_device_info = device_info
|
self.__device_info = self.__saved_device_info = device_info
|
||||||
|
|||||||
@@ -40,8 +40,6 @@ import aiohttp
|
|||||||
import aiohttp.web
|
import aiohttp.web
|
||||||
import setproctitle
|
import setproctitle
|
||||||
|
|
||||||
from ... import aiotools
|
|
||||||
|
|
||||||
from ...logging import get_logger
|
from ...logging import get_logger
|
||||||
|
|
||||||
from ...aioregion import RegionIsBusyError
|
from ...aioregion import RegionIsBusyError
|
||||||
@@ -65,6 +63,8 @@ from ...validators.kvm import valid_hid_mouse_move
|
|||||||
from ...validators.kvm import valid_hid_mouse_button
|
from ...validators.kvm import valid_hid_mouse_button
|
||||||
from ...validators.kvm import valid_hid_mouse_wheel
|
from ...validators.kvm import valid_hid_mouse_wheel
|
||||||
|
|
||||||
|
from ... import aiotools
|
||||||
|
|
||||||
from ... import __version__
|
from ... import __version__
|
||||||
|
|
||||||
from .auth import AuthManager
|
from .auth import AuthManager
|
||||||
|
|||||||
Reference in New Issue
Block a user