From 70821ee47b74eb50f70bd085b4fc88b316b1c042 Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Thu, 16 Jun 2022 19:09:06 +0200 Subject: Add stop functionality --- backend/plugin/python_plugin.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'backend/plugin/python_plugin.py') diff --git a/backend/plugin/python_plugin.py b/backend/plugin/python_plugin.py index c2833088..f7270e2d 100644 --- a/backend/plugin/python_plugin.py +++ b/backend/plugin/python_plugin.py @@ -53,6 +53,8 @@ class PythonPlugin: self.client = PluginProtocolClient(self, logger) self.server = PluginProtocolServer(self) self.connection = None + self.process = None + self.stopped = False self.plugin_directory = plugin_directory self.file_name = file_name @@ -93,12 +95,22 @@ class PythonPlugin: self.process = multiprocessing.Process(target=self._init) self.process.start() get_event_loop().create_task(self.process_loop()) + self.stopped = False + + async def stop(self): + self.stopped = True + if self.connection: + self.connection.close() + + if self.process and self.process.is_alive: + self.process.terminate() async def process_loop(self): await get_event_loop().run_in_executor(None, self.process.join) - self.logger.info("backend process was killed - restarting in 10 seconds") - await sleep(10) - await self.start() + if not self.stopped: + self.logger.info("backend process was killed - restarting in 10 seconds") + await sleep(10) + await self.start() # called on the server/loader process async def call_method(self, method_name, method_args): -- cgit v1.2.3