diff options
author | Devaev Maxim <[email protected]> | 2019-06-06 04:04:13 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2019-06-06 04:04:13 +0300 |
commit | 288fb05ea544bb678115cca66c51efb4dc3d909b (patch) | |
tree | 5aecedb5fa24acabcb5f363641dc8b70e40c6283 | |
parent | 6d7996924f98bc33e6492e0fa9e36c321d8fb969 (diff) |
refactoring
-rw-r--r-- | kvmd/aiotools.py | 15 | ||||
-rw-r--r-- | kvmd/apps/kvmd/auth.py | 4 | ||||
-rw-r--r-- | kvmd/apps/kvmd/info.py | 7 | ||||
-rw-r--r-- | kvmd/apps/kvmd/msd.py | 8 | ||||
-rw-r--r-- | kvmd/apps/kvmd/server.py | 4 |
5 files changed, 25 insertions, 13 deletions
diff --git a/kvmd/aiotools.py b/kvmd/aiotools.py index b164e110..ef29a2df 100644 --- a/kvmd/aiotools.py +++ b/kvmd/aiotools.py @@ -37,14 +37,14 @@ _AtomicF = TypeVar("_AtomicF", bound=Callable[..., Any]) def atomic(method: _AtomicF) -> _AtomicF: @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 typing.cast(_AtomicF, wrapper) def tasked(method: Callable[..., Any]) -> Callable[..., asyncio.Task]: @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 typing.cast(Callable[..., asyncio.Task], wrapper) @@ -64,3 +64,14 @@ async def gather_short_tasks() -> None: for task in asyncio.Task.all_tasks() 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) diff --git a/kvmd/apps/kvmd/auth.py b/kvmd/apps/kvmd/auth.py index 6125db29..e4268e74 100644 --- a/kvmd/apps/kvmd/auth.py +++ b/kvmd/apps/kvmd/auth.py @@ -26,13 +26,13 @@ from typing import List from typing import Dict from typing import Optional -from ... import aiotools - from ...logging import get_logger from ...plugins.auth import BaseAuthService from ...plugins.auth import get_auth_service_class +from ... import aiotools + # ===== class AuthManager: diff --git a/kvmd/apps/kvmd/info.py b/kvmd/apps/kvmd/info.py index 5e76e3c8..088927e3 100644 --- a/kvmd/apps/kvmd/info.py +++ b/kvmd/apps/kvmd/info.py @@ -21,7 +21,6 @@ import os -import asyncio import contextlib from typing import Dict @@ -33,6 +32,8 @@ from ...logging import get_logger from ...yamlconf.loader import load_yaml_file +from ... import aiotools + # ===== class InfoManager: @@ -46,10 +47,10 @@ class InfoManager: self.__extras_path = extras_path 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: - 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: extras: Dict[str, Dict] = {} diff --git a/kvmd/apps/kvmd/msd.py b/kvmd/apps/kvmd/msd.py index 49c16651..995f64da 100644 --- a/kvmd/apps/kvmd/msd.py +++ b/kvmd/apps/kvmd/msd.py @@ -41,8 +41,8 @@ import aiofiles.base from ...logging import get_logger -from ... import aioregion from ... import aiotools +from ... import aioregion 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) try: 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: if isinstance(err, MsdError): log = logger.error @@ -374,10 +374,10 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes assert self.__device_file await self.__device_file.write(data) 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: - 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: raise MsdError("Can't explore device %r" % (self._device_path)) self.__device_info = self.__saved_device_info = device_info diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py index 18e8b65f..67207152 100644 --- a/kvmd/apps/kvmd/server.py +++ b/kvmd/apps/kvmd/server.py @@ -40,8 +40,6 @@ import aiohttp import aiohttp.web import setproctitle -from ... import aiotools - from ...logging import get_logger 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_wheel +from ... import aiotools + from ... import __version__ from .auth import AuthManager |