mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-31 01:51:53 +08:00
refactoring
This commit is contained in:
@@ -87,7 +87,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
|||||||
|
|
||||||
self.__loop = loop
|
self.__loop = loop
|
||||||
|
|
||||||
self.__proc_task: Optional[asyncio.Task] = None
|
self.__streamer_task: Optional[asyncio.Task] = None
|
||||||
|
|
||||||
self.__http_session: Optional[aiohttp.ClientSession] = None
|
self.__http_session: Optional[aiohttp.ClientSession] = None
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
|||||||
await self.__inner_stop()
|
await self.__inner_stop()
|
||||||
|
|
||||||
def is_running(self) -> bool:
|
def is_running(self) -> bool:
|
||||||
return bool(self.__proc_task)
|
return bool(self.__streamer_task)
|
||||||
|
|
||||||
def get_params(self) -> Dict:
|
def get_params(self) -> Dict:
|
||||||
return dict(self.__params)
|
return dict(self.__params)
|
||||||
@@ -166,16 +166,16 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
|||||||
self.__http_session = aiohttp.ClientSession()
|
self.__http_session = aiohttp.ClientSession()
|
||||||
|
|
||||||
async def __inner_start(self) -> None:
|
async def __inner_start(self) -> None:
|
||||||
assert not self.__proc_task
|
assert not self.__streamer_task
|
||||||
await self.__set_hw_enabled(True)
|
await self.__set_hw_enabled(True)
|
||||||
self.__proc_task = self.__loop.create_task(self.__process())
|
self.__streamer_task = self.__loop.create_task(self.__run_streamer())
|
||||||
|
|
||||||
async def __inner_stop(self) -> None:
|
async def __inner_stop(self) -> None:
|
||||||
assert self.__proc_task
|
assert self.__streamer_task
|
||||||
self.__proc_task.cancel()
|
self.__streamer_task.cancel()
|
||||||
await asyncio.gather(self.__proc_task, return_exceptions=True)
|
await asyncio.gather(self.__streamer_task, return_exceptions=True)
|
||||||
await self.__set_hw_enabled(False)
|
await self.__set_hw_enabled(False)
|
||||||
self.__proc_task = None
|
self.__streamer_task = None
|
||||||
|
|
||||||
async def __set_hw_enabled(self, enabled: bool) -> None:
|
async def __set_hw_enabled(self, enabled: bool) -> None:
|
||||||
# XXX: This sequence is very important to enable converter and cap board
|
# XXX: This sequence is very important to enable converter and cap board
|
||||||
@@ -188,21 +188,13 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
|||||||
if enabled:
|
if enabled:
|
||||||
await asyncio.sleep(self.__init_delay)
|
await asyncio.sleep(self.__init_delay)
|
||||||
|
|
||||||
async def __process(self) -> None: # pylint: disable=too-many-branches
|
async def __run_streamer(self) -> None: # pylint: disable=too-many-branches
|
||||||
logger = get_logger(0)
|
logger = get_logger(0)
|
||||||
|
|
||||||
while True: # pylint: disable=too-many-nested-blocks
|
while True: # pylint: disable=too-many-nested-blocks
|
||||||
proc: Optional[asyncio.subprocess.Process] = None # pylint: disable=no-member
|
proc: Optional[asyncio.subprocess.Process] = None # pylint: disable=no-member
|
||||||
try:
|
try:
|
||||||
cmd = [
|
cmd = self.__make_cmd()
|
||||||
part.format(
|
|
||||||
host=self.__host,
|
|
||||||
port=self.__port,
|
|
||||||
unix=self.__unix_path,
|
|
||||||
**self.__params,
|
|
||||||
)
|
|
||||||
for part in self.__cmd
|
|
||||||
]
|
|
||||||
proc = await asyncio.create_subprocess_exec(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
*cmd,
|
*cmd,
|
||||||
stdout=asyncio.subprocess.PIPE,
|
stdout=asyncio.subprocess.PIPE,
|
||||||
@@ -238,6 +230,17 @@ class Streamer: # pylint: disable=too-many-instance-attributes
|
|||||||
if proc and proc.returncode is None:
|
if proc and proc.returncode is None:
|
||||||
await self.__kill(proc)
|
await self.__kill(proc)
|
||||||
|
|
||||||
|
def __make_cmd(self) -> List[str]:
|
||||||
|
return [
|
||||||
|
part.format(
|
||||||
|
host=self.__host,
|
||||||
|
port=self.__port,
|
||||||
|
unix=self.__unix_path,
|
||||||
|
**self.__params,
|
||||||
|
)
|
||||||
|
for part in self.__cmd
|
||||||
|
]
|
||||||
|
|
||||||
async def __kill(self, proc: asyncio.subprocess.Process) -> None: # pylint: disable=no-member
|
async def __kill(self, proc: asyncio.subprocess.Process) -> None: # pylint: disable=no-member
|
||||||
try:
|
try:
|
||||||
proc.terminate()
|
proc.terminate()
|
||||||
|
|||||||
Reference in New Issue
Block a user