diff options
| author | marios8543 <marios8543@gmail.com> | 2023-10-18 19:38:07 +0300 |
|---|---|---|
| committer | marios8543 <marios8543@gmail.com> | 2023-10-18 19:38:07 +0300 |
| commit | feabb582b2def5ad437a57dc9600d398d32fcfab (patch) | |
| tree | 33cc3a2603177b55dbf776335254fb08a09d4464 /backend/src/localplatform/localsocket.py | |
| parent | 47e9708a209c9f48159880b90a710ea26ec09a29 (diff) | |
| download | decky-loader-feabb582b2def5ad437a57dc9600d398d32fcfab.tar.gz decky-loader-feabb582b2def5ad437a57dc9600d398d32fcfab.zip | |
run method calls asynchronously
Diffstat (limited to 'backend/src/localplatform/localsocket.py')
| -rw-r--r-- | backend/src/localplatform/localsocket.py | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/backend/src/localplatform/localsocket.py b/backend/src/localplatform/localsocket.py index f38fe5e7..2ef039ee 100644 --- a/backend/src/localplatform/localsocket.py +++ b/backend/src/localplatform/localsocket.py @@ -1,5 +1,5 @@ import asyncio, time -from typing import Awaitable, Callable +from typing import Any, Callable, Coroutine import random from .localplatform import ON_WINDOWS @@ -7,7 +7,7 @@ from .localplatform import ON_WINDOWS BUFFER_LIMIT = 2 ** 20 # 1 MiB class UnixSocket: - def __init__(self, on_new_message: Callable[[str], Awaitable[str|None]]): + def __init__(self, on_new_message: Callable[[str], Coroutine[Any, Any, Any]]): ''' on_new_message takes 1 string argument. It's return value gets used, if not None, to write data to the socket. @@ -93,18 +93,17 @@ class UnixSocket: async def _listen_for_method_call(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter): while True: - line = await self._read_single_line(reader) - try: - res = await self.on_new_message(line) - except Exception: - return + def _(task: asyncio.Task[str|None]): + res = task.result() + if res is not None: + asyncio.create_task(self._write_single_line(writer, res)) - if res != None: - await self._write_single_line(writer, res) + line = await self._read_single_line(reader) + asyncio.create_task(self.on_new_message(line)).add_done_callback(_) class PortSocket (UnixSocket): - def __init__(self, on_new_message: Callable[[str], Awaitable[str|None]]): + def __init__(self, on_new_message: Callable[[str], Coroutine[Any, Any, Any]]): ''' on_new_message takes 1 string argument. It's return value gets used, if not None, to write data to the socket. |
