From 17f003ca9df502be538b934689529bad3d1bc894 Mon Sep 17 00:00:00 2001 From: xfangfang <2553041586@qq.com> Date: Mon, 16 Aug 2021 21:12:29 +0800 Subject: [PATCH] Improve the stability of event subscription --- macast/mpv.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/macast/mpv.py b/macast/mpv.py index b305b2b82..438e6e9bb 100644 --- a/macast/mpv.py +++ b/macast/mpv.py @@ -191,6 +191,7 @@ def __init__(self): self.eventSubcribes = {} # subscribe devices self.stateQueue = Queue() # states needed be send to subscribe devices self.removedDeviceQueue = Queue() # devices needed be removed + self.appendDeviceQueue = Queue() # devices needed be added self._buildAction('AVTransport', self.av_transport) self._buildAction('RenderingControl', self.rendering_control) self._buildAction('ConnectionManager', self.conection_manager) @@ -221,7 +222,7 @@ def addSubcribe(self, service, url, timeout=1800): } logger.error("SUBSCRIBE ADD") client = ObserveClient(service, url, timeout) - self.eventSubcribes[client.sid] = client + self.appendDeviceQueue.put(client) threading.Thread(target=self._sendInitEvent, kwargs={ 'service': service, @@ -265,6 +266,11 @@ def _eventCallback(self, stateChangeList): logger.info("Remove client: {}".format(sid)) del self.eventSubcribes[sid] self.removedDeviceQueue.task_done() + # add clients + while not self.appendDeviceQueue.empty(): + client = self.appendDeviceQueue.get() + self.eventSubcribes[client.sid] = client + self.appendDeviceQueue.task_done() # send stateChangeList to client for sid in self.eventSubcribes: client = self.eventSubcribes[sid]