From 315b2f9cda6def0c93bc7a0fa302415d2f972c85 Mon Sep 17 00:00:00 2001 From: marios8543 Date: Wed, 18 Oct 2023 00:04:14 +0300 Subject: [PATCH] Run response_listener task --- backend/src/plugin/plugin.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/backend/src/plugin/plugin.py b/backend/src/plugin/plugin.py index b7d0a44af..d5f79d42a 100644 --- a/backend/src/plugin/plugin.py +++ b/backend/src/plugin/plugin.py @@ -1,3 +1,4 @@ +from asyncio import Task, create_task from json import dumps, load, loads from logging import getLogger from os import path @@ -31,12 +32,12 @@ def __init__(self, file: str, plugin_directory: str, plugin_path: str) -> None: self.sandboxed_plugin = SandboxedPlugin(self.name, self.passive, self.flags, self.file, self.plugin_directory, self.plugin_path, self.version, self.author) #TODO: Maybe somehow make LocalSocket not require on_new_message to make this more clear self.socket = LocalSocket(self.sandboxed_plugin.on_new_message) - self.sandboxed_plugin.start(self.socket) + self.listener_task: Task[Any] def __str__(self) -> str: return self.name - async def response_listener(self): + async def _response_listener(self): while True: line = await self.socket.read_single_line() if line != None: @@ -52,4 +53,15 @@ async def execute_method(self, method_name: str, kwargs: Dict[Any, Any]): await self.socket.write_single_line(dumps({ "method": method_name, "args": kwargs, "id": request.id }, ensure_ascii=False)) self.method_call_requests[request.id] = request - return await request.wait_for_result() \ No newline at end of file + return await request.wait_for_result() + + async def start(self): + self.sandboxed_plugin.start(self.socket) + self.listener_task = create_task(self._response_listener()) + + async def stop(self): + self.listener_task.cancel() + async def _(self: PluginWrapper): + await self.socket.write_single_line(dumps({ "stop": True }, ensure_ascii=False)) + await self.socket.close_socket_connection() + create_task(_(self)) \ No newline at end of file