diff options
| author | marios8543 <marios8543@gmail.com> | 2023-10-18 19:38:07 +0300 |
|---|---|---|
| committer | marios8543 <marios8543@gmail.com> | 2023-10-31 23:18:31 +0200 |
| commit | e4b1efc44dc2f8dc718c809ea50680de626474c4 (patch) | |
| tree | 8012bc64ca7278b7cb7ad335dd718e98822b9f02 /backend/src/localplatform/localsocket.py | |
| parent | 85f4604bfdf3b5403f5f65e0c1e7f512b5b95ec1 (diff) | |
| download | decky-loader-e4b1efc44dc2f8dc718c809ea50680de626474c4.tar.gz decky-loader-e4b1efc44dc2f8dc718c809ea50680de626474c4.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. |
