From 47d17a17a880c9399753b719a27d0dfc6565bac3 Mon Sep 17 00:00:00 2001 From: huanglf Date: Sat, 9 Mar 2024 20:32:30 +0800 Subject: [PATCH] Fix NNGException when msg port is not ready yet --- clients/python/wcferry/client.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/clients/python/wcferry/client.py b/clients/python/wcferry/client.py index 7452d671..7c41202c 100644 --- a/clients/python/wcferry/client.py +++ b/clients/python/wcferry/client.py @@ -518,7 +518,6 @@ def enable_receiving_msg(self, pyq=False) -> bool: """允许接收消息,成功后通过 `get_msg` 读取消息""" def listening_msg(): rsp = wcf_pb2.Response() - self.msg_socket.dial(self.msg_url, block=True) while self._is_receiving_msg: try: rsp.ParseFromString(self.msg_socket.recv_msg().bytes) @@ -540,6 +539,19 @@ def listening_msg(): if rsp.status != 0: return False + fail_cnt = 0 + while True: + try: + self.msg_socket.dial(self.msg_url, block=True) + break + except pynng.exceptions.NNGException as e: + if fail_cnt > 50: + raise e + fail_cnt += 1 + sleep(0.1) # 等待消息端口启动监听 + except Exception as e: + raise e + self._is_receiving_msg = True # 阻塞,把控制权交给用户 # self.listening_msg(callback)