From f81b8a0745b62c6694825309035ef8179a380566 Mon Sep 17 00:00:00 2001 From: trivialkettle <126059809+trivialkettle@users.noreply.github.com> Date: Tue, 15 Oct 2024 16:39:09 +0200 Subject: [PATCH] Remove busy loop from rx_thread The rx_thread executed a busy loop and loaded a single core to 100%. This change reduces the load significantly. --- pyusbtin/usbtin.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pyusbtin/usbtin.py b/pyusbtin/usbtin.py index 679353b..6aa5e0e 100644 --- a/pyusbtin/usbtin.py +++ b/pyusbtin/usbtin.py @@ -274,7 +274,11 @@ def rx_thread(self): while self.rx_thread_state == USBtin.RX_THREAD_RUNNING: # fetch bytes if available rxcount = self.serial_port.inWaiting() - if rxcount > 0: + if rxcount == 0: + # if no data is available, sleep for 10ms to avoid busy looping + sleep(0.01) + continue + else: # fetch all data from serial buffer buf = self.serial_port.read(rxcount) if PY_VERSION == 2: