From 1318ea50eef63d5ccfa945d702f3bb9a8e19a9c7 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 9 Nov 2022 21:37:46 +0800 Subject: [PATCH] bugfix --- appPublic/udp_comm.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/appPublic/udp_comm.py b/appPublic/udp_comm.py index 8798b84..4e9a810 100644 --- a/appPublic/udp_comm.py +++ b/appPublic/udp_comm.py @@ -1,4 +1,5 @@ # -*- coding:UTF-8 -*- +import time from traceback import print_exc from socket import * import json @@ -6,16 +7,16 @@ from appPublic.sockPackage import get_free_local_addr from appPublic.background import Background BUFSIZE = 1024 * 64 class UdpComm: - def __init__(self, port, callback, timeout=1): + def __init__(self, port, callback, timeout=0): self.callback = callback self.timeout = timeout self.host = get_free_local_addr()[0] self.port = port self.udpSerSock = socket(AF_INET, SOCK_DGRAM) # 设置阻塞 - self.udpSerSock.setblocking(1) + self.udpSerSock.setblocking(1 if timeout > 0 else 0) # 设置超时时间 1s - # self.udpSerSock.settimeout(timeout) + self.udpSerSock.settimeout(timeout) self.udpSerSock.bind(('' ,port)) self.run_flg = True self.thread = Background(self.run) @@ -27,6 +28,8 @@ class UdpComm: b, addr = self.udpSerSock.recvfrom(BUFSIZE) if addr[0] != self.host: self.callback(b, addr) + if timeout == 0: + time.sleep(0.1) except Exception as e: print('exception happened:',e) print_exc()