From 2444545dc167bacd16a15c1a5c7e10508cba9182 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Mon, 6 Jun 2022 17:22:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=81=BF=E5=85=8D=E9=80=80=E5=87=BA=E6=97=B6?= =?UTF-8?q?=EF=BC=8Coutput=E9=98=BB=E5=A1=9E=E9=80=80=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- output/manager.py | 9 +++++++++ proxy/queues.py | 8 ++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/output/manager.py b/output/manager.py index e00b65b..79519c7 100644 --- a/output/manager.py +++ b/output/manager.py @@ -31,6 +31,7 @@ class OutputManager(): } _writer: "List[IOutput]" = [] _thread: "Optional[threading.Thread]"= None + _should_exit = threading.Event() def __init__(self): _config = config()['output']['use'] @@ -107,14 +108,22 @@ class OutputManager(): writer.error_output(message.method, message.payload, e) def start_loop(self): + self._should_exit.clear() self._thread = threading.Thread(target=self._handle) self._thread.start() def _handle(self): while True: message = MESSAGE_QUEUE.get() + if self._should_exit.is_set(): + break + if message is None: + continue self.decode_payload(message) def terminate(self): + self._should_exit.set() + MESSAGE_QUEUE.put(None) + for writer in self._writer: writer.terminate() diff --git a/proxy/queues.py b/proxy/queues.py index ac037ba..a9cf669 100644 --- a/proxy/queues.py +++ b/proxy/queues.py @@ -1,4 +1,8 @@ from queue import SimpleQueue -from proxy.common import MessagePayload +from typing import TYPE_CHECKING -MESSAGE_QUEUE: "SimpleQueue[MessagePayload]" = SimpleQueue() +if TYPE_CHECKING: + from typing import Optional + from proxy.common import MessagePayload + +MESSAGE_QUEUE: "SimpleQueue[Optional[MessagePayload]]" = SimpleQueue()