This commit is contained in:
Devaev Maxim 2018-12-09 01:37:32 +03:00
parent 197e7cad6a
commit 014884f9ac
2 changed files with 26 additions and 10 deletions

View File

@ -1,3 +1,5 @@
import os
import signal
import asyncio import asyncio
import multiprocessing import multiprocessing
import multiprocessing.queues import multiprocessing.queues
@ -135,6 +137,7 @@ class Hid(multiprocessing.Process): # pylint: disable=too-many-instance-attribu
self.__pressed_keys.clear() self.__pressed_keys.clear()
def __emergency_clear_events(self) -> None: def __emergency_clear_events(self) -> None:
if os.path.exists(self.__device_path):
try: try:
with serial.Serial(self.__device_path, self.__speed) as tty: with serial.Serial(self.__device_path, self.__speed) as tty:
self.__send_clear_hid(tty) self.__send_clear_hid(tty)
@ -142,6 +145,7 @@ class Hid(multiprocessing.Process): # pylint: disable=too-many-instance-attribu
get_logger().exception("Can't execute emergency clear HID events") get_logger().exception("Can't execute emergency clear HID events")
def run(self) -> None: # pylint: disable=too-many-branches def run(self) -> None: # pylint: disable=too-many-branches
signal.signal(signal.SIGINT, signal.SIG_IGN)
setproctitle.setproctitle("[hid] " + setproctitle.getproctitle()) setproctitle.setproctitle("[hid] " + setproctitle.getproctitle())
try: try:
with serial.Serial(self.__device_path, self.__speed) as tty: with serial.Serial(self.__device_path, self.__speed) as tty:

View File

@ -1,4 +1,5 @@
import os import os
import signal
import asyncio import asyncio
import asyncio.subprocess import asyncio.subprocess
@ -61,13 +62,11 @@ class Streamer: # pylint: disable=too-many-instance-attributes
self.__cmd = cmd self.__cmd = cmd
self.__loop = loop self.__loop = loop
if self.__unix_path:
self.__http_session = aiohttp.ClientSession(connector=aiohttp.UnixConnector(path=self.__unix_path))
else:
self.__http_session = aiohttp.ClientSession()
self.__proc_task: Optional[asyncio.Task] = None self.__proc_task: Optional[asyncio.Task] = None
self.__http_session: Optional[aiohttp.ClientSession] = None
async def start(self, params: Dict, no_init_restart: bool=False) -> None: async def start(self, params: Dict, no_init_restart: bool=False) -> None:
logger = get_logger() logger = get_logger()
logger.info("Starting streamer ...") logger.info("Starting streamer ...")
@ -94,10 +93,11 @@ class Streamer: # pylint: disable=too-many-instance-attributes
return dict(self.__params) return dict(self.__params)
async def get_state(self) -> Dict: async def get_state(self) -> Dict:
self.__ensure_session()
url = "http://%s:%d/state" % (self.__host, self.__port) url = "http://%s:%d/state" % (self.__host, self.__port)
state = None state = None
try: try:
async with self.__http_session.get(url, timeout=self.__timeout) as response: async with self.__http_session.get(url, timeout=self.__timeout) as response: # type: ignore
response.raise_for_status() response.raise_for_status()
state = (await response.json())["result"] state = (await response.json())["result"]
except aiohttp.ClientConnectorError: except aiohttp.ClientConnectorError:
@ -122,6 +122,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
*[self.__cmd[0], "--version"], *[self.__cmd[0], "--version"],
stdout=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.DEVNULL, stderr=asyncio.subprocess.DEVNULL,
preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)),
) )
(stdout, _) = await proc.communicate() (stdout, _) = await proc.communicate()
return stdout.decode(errors="ignore").strip() return stdout.decode(errors="ignore").strip()
@ -129,6 +130,16 @@ class Streamer: # pylint: disable=too-many-instance-attributes
async def cleanup(self) -> None: async def cleanup(self) -> None:
if self.is_running(): if self.is_running():
await self.stop() await self.stop()
if self.__http_session:
await self.__http_session.close()
self.__http_session = None
def __ensure_session(self) -> None:
if not self.__http_session:
if self.__unix_path:
self.__http_session = aiohttp.ClientSession(connector=aiohttp.UnixConnector(path=self.__unix_path))
else:
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.__proc_task
@ -172,6 +183,7 @@ class Streamer: # pylint: disable=too-many-instance-attributes
*cmd, *cmd,
stdout=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT, stderr=asyncio.subprocess.STDOUT,
preexec_fn=(lambda: signal.signal(signal.SIGINT, signal.SIG_IGN)),
) )
logger.info("Started streamer pid=%d: %s", proc.pid, cmd) logger.info("Started streamer pid=%d: %s", proc.pid, cmd)