I use a thread to read from a UDP socket. Afterwards the string gets parsed. In another thread I send to every connected client via this socket. I would like to keep latency and resources for running the script as low as possible.
It would be nice to add a time-out.
Every attempt to read should be cancelled if longer than e.g. 20 ms, to keep the response time for other clients low, because I believe that the current attempt to read from this socket is blocking the loop. I was reading that some people use select() is there an advantage?
def trnm_thr(): # trnm_thr() sends commands to Arduino
global udp_clients
udp_client = None
while pySerial is not None:
if not pySerial.writable():
continue
try:
udp_msg, udp_client = udp_sock.recvfrom(512) # Wait for UDP packet from ground station
if udp_client is not None: udp_clients.add(udp_client) # Add new client to client list
if not udp_msg: continue # If message is empty continue without parsing
except socket.timeout:
logging.error("Write timeout on socket") # Log the problem
else:
try:
p = json.loads(udp_msg) # parse JSON string from socket
except (ValueError, KeyError, TypeError):
logging.debug("JSON format error: " + udp_msg.strip() )
else:
# Serial device stuff ..