From 6e3edb5b304130a5d58fe28ce0ab0b9db6a7f3f2 Mon Sep 17 00:00:00 2001 From: DJ2LS <75909252+DJ2LS@users.noreply.github.com> Date: Mon, 12 Dec 2022 12:28:52 +0100 Subject: [PATCH] attempt using threading.Event().wait() instead of time.sleep() --- tnc/daemon.py | 6 +++--- tnc/data_handler.py | 46 ++++++++++++++++++++++----------------------- tnc/explorer.py | 2 +- tnc/helpers.py | 3 ++- tnc/main.py | 2 +- tnc/modem.py | 8 ++++---- tnc/rigctld.py | 3 ++- tnc/sock.py | 8 ++++---- 8 files changed, 40 insertions(+), 38 deletions(-) diff --git a/tnc/daemon.py b/tnc/daemon.py index a2af67a5..7a013b4f 100755 --- a/tnc/daemon.py +++ b/tnc/daemon.py @@ -93,7 +93,7 @@ class DAEMON: "[DMN] update_audio_devices: Exception gathering audio devices:", e=err1, ) - time.sleep(1) + threading.Event().wait(1) def update_serial_devices(self): """ @@ -114,7 +114,7 @@ class DAEMON: ) static.SERIAL_DEVICES = serial_devices - time.sleep(1) + threading.Event().wait(1) except Exception as err1: self.log.error( "[DMN] update_serial_devices: Exception gathering serial devices:", @@ -443,4 +443,4 @@ if __name__ == "__main__": version=static.VERSION, ) while True: - time.sleep(1) + threading.Event().wait(1) diff --git a/tnc/data_handler.py b/tnc/data_handler.py index dbdd0d4b..01babfdc 100644 --- a/tnc/data_handler.py +++ b/tnc/data_handler.py @@ -402,7 +402,7 @@ class DATA: # Wait while transmitting while static.TRANSMITTING: - time.sleep(0.01) + threading.Event().wait(0.01) def send_data_to_socket_queue(self, **jsondata): """ @@ -1076,7 +1076,7 @@ class DATA: # while (not self.burst_ack and not self.burst_nack and # not self.rpt_request_received and not self.data_frame_ack_received and # time.time() < burstacktimeout and static.ARQ_STATE): - # time.sleep(0.01) + # threading.Event().wait(0.01) # burstacktimeout = time.time() + self.burst_ack_timeout_seconds + 100 while static.ARQ_STATE and not ( @@ -1085,7 +1085,7 @@ class DATA: or self.rpt_request_received or self.data_frame_ack_received ): - time.sleep(0.01) + threading.Event().wait(0.01) # Once we receive a burst ack, reset its state and break the RETRIES loop if self.burst_ack: @@ -1162,7 +1162,7 @@ class DATA: # we need to wait until sending "transmitted" state # gui database is too slow for handling this within 0.001 seconds # so let's sleep a little - time.sleep(0.2) + threading.Event().wait(0.2) self.send_data_to_socket_queue( freedata="tnc-message", arq="transmission", @@ -1402,7 +1402,7 @@ class DATA: # wait while timeout not reached and our busy state is busy channel_busy_timeout = time.time() + 30 while static.CHANNEL_BUSY and time.time() < channel_busy_timeout: - time.sleep(0.01) + threading.Event().wait(0.01) # if channel busy timeout reached stop connecting if time.time() > channel_busy_timeout: @@ -1422,7 +1422,7 @@ class DATA: # wait until data channel is open while not static.ARQ_SESSION and not self.arq_session_timeout: - time.sleep(0.01) + threading.Event().wait(0.01) static.ARQ_SESSION_STATE = "connecting" self.send_data_to_socket_queue( freedata="tnc-message", @@ -1486,7 +1486,7 @@ class DATA: connection_frame[8:14] = helpers.callsign_to_bytes(self.mycallsign) while not static.ARQ_SESSION: - time.sleep(0.01) + threading.Event().wait(0.01) for attempt in range(self.session_connect_max_retries): self.log.info( "[TNC] SESSION [" @@ -1514,7 +1514,7 @@ class DATA: # indicates we've received a positive response from the far station. timeout = time.time() + 3 while time.time() < timeout: - time.sleep(0.01) + threading.Event().wait(0.01) # Stop waiting if data channel is opened if static.ARQ_SESSION: return True @@ -1790,7 +1790,7 @@ class DATA: # wait a moment for the case, a heartbeat is already on the way back to us # this makes channel establishment more clean if static.ARQ_SESSION: - time.sleep(2) + threading.Event().wait(2) self.datachannel_timeout = False @@ -1813,7 +1813,7 @@ class DATA: # wait while timeout not reached and our busy state is busy channel_busy_timeout = time.time() + 30 while static.CHANNEL_BUSY and time.time() < channel_busy_timeout: - time.sleep(0.01) + threading.Event().wait(0.01) # if channel busy timeout reached, stop connecting if time.time() > channel_busy_timeout: @@ -1834,7 +1834,7 @@ class DATA: # wait until data channel is open while not static.ARQ_STATE and not self.datachannel_timeout: - time.sleep(0.01) + threading.Event().wait(0.01) if static.ARQ_STATE: self.arq_transmit(data_out, mode, n_frames_per_burst) @@ -1884,7 +1884,7 @@ class DATA: connection_frame[13:14] = self.session_id while not static.ARQ_STATE: - time.sleep(0.01) + threading.Event().wait(0.01) for attempt in range(self.data_channel_max_retries): self.send_data_to_socket_queue( @@ -1908,7 +1908,7 @@ class DATA: timeout = time.time() + 3 while time.time() < timeout: - time.sleep(0.01) + threading.Event().wait(0.01) # Stop waiting if data channel is opened if static.ARQ_STATE: return True @@ -2406,7 +2406,7 @@ class DATA: """ try: while True: - time.sleep(0.5) + threading.Event().wait(0.5) while static.BEACON_STATE: if ( not static.ARQ_SESSION @@ -2444,7 +2444,7 @@ class DATA: and static.BEACON_STATE and not static.BEACON_PAUSE ): - time.sleep(0.01) + threading.Event().wait(0.01) except Exception as err: self.log.debug("[TNC] run_beacon: ", exception=err) @@ -2845,7 +2845,7 @@ class DATA: """ while True: - time.sleep(0.1) + threading.Event().wait(0.1) self.data_channel_keep_alive_watchdog() self.burst_watchdog() self.arq_session_keep_alive_watchdog() @@ -2919,7 +2919,7 @@ class DATA: """ # and not static.ARQ_SEND_KEEP_ALIVE: if static.ARQ_STATE and static.TNC_STATE == "BUSY": - time.sleep(0.01) + threading.Event().wait(0.01) if ( self.data_channel_last_received + self.transmission_timeout > time.time() @@ -2927,7 +2927,7 @@ class DATA: timeleft = (self.data_channel_last_received + self.transmission_timeout) - time.time() self.log.debug("Time left until timeout", seconds=timeleft) - time.sleep(5) + threading.Event().wait(5) # print(self.data_channel_last_received + self.transmission_timeout - time.time()) # pass else: @@ -2961,7 +2961,7 @@ class DATA: and not self.arq_file_transfer ): if self.arq_session_last_received + self.arq_session_timeout > time.time(): - time.sleep(0.01) + threading.Event().wait(0.01) else: self.log.info( "[TNC] SESSION [" @@ -2985,19 +2985,19 @@ class DATA: Heartbeat thread which auto pauses and resumes the heartbeat signal when in an arq session """ while True: - time.sleep(0.01) + threading.Event().wait(0.01) # additional check for smoother stopping if heartbeat transmission while not self.arq_file_transfer: - time.sleep(0.01) + threading.Event().wait(0.01) if ( static.ARQ_SESSION and self.IS_ARQ_SESSION_MASTER and static.ARQ_SESSION_STATE == "connected" # and not self.arq_file_transfer ): - time.sleep(1) + threading.Event().wait(1) self.transmit_session_heartbeat() - time.sleep(2) + threading.Event().wait(2) def send_test_frame(self) -> None: """Send an empty test frame""" diff --git a/tnc/explorer.py b/tnc/explorer.py index 56dd01d4..bed33e80 100644 --- a/tnc/explorer.py +++ b/tnc/explorer.py @@ -28,7 +28,7 @@ class explorer(): def interval(self): while True: self.push() - time.sleep(self.publish_interval) + threading.Event().wait(self.publish_interval) def push(self): diff --git a/tnc/helpers.py b/tnc/helpers.py index 449b11c3..962e4d28 100644 --- a/tnc/helpers.py +++ b/tnc/helpers.py @@ -10,6 +10,7 @@ import crcengine import static import structlog import numpy as np +import threading log = structlog.get_logger("helpers") @@ -25,7 +26,7 @@ def wait(seconds: float) -> bool: timeout = time.time() + seconds while time.time() < timeout: - time.sleep(0.01) + threading.Event().wait(0.01) return True diff --git a/tnc/main.py b/tnc/main.py index f21edfb9..49f7a2c6 100755 --- a/tnc/main.py +++ b/tnc/main.py @@ -437,4 +437,4 @@ if __name__ == "__main__": log.error("[TNC] Starting TCP/IP socket failed", port=static.PORT, e=err) sys.exit(1) while True: - time.sleep(1) + threading.Event().wait(1) diff --git a/tnc/modem.py b/tnc/modem.py index 10031344..d40f7d2e 100644 --- a/tnc/modem.py +++ b/tnc/modem.py @@ -291,7 +291,7 @@ class RF: depositing the data into the codec data buffers. """ while True: - time.sleep(0.01) + threading.Event().wait(0.01) # -----read data_in48k = bytes() with open(RXCHANNEL, "rb") as fifo: @@ -322,7 +322,7 @@ class RF: def mkfifo_write_callback(self) -> None: """Support testing by writing the audio data to a pipe.""" while True: - time.sleep(0.01) + threading.Event().wait(0.01) # -----write if len(self.modoutqueue) <= 0 or self.mod_out_locked: @@ -529,7 +529,7 @@ class RF: self.mod_out_locked = False while self.modoutqueue: - time.sleep(0.01) + threading.Event().wait(0.01) static.PTT_STATE = self.hamlib.set_ptt(False) @@ -887,7 +887,7 @@ class RF: rms_counter = 0 while True: - # time.sleep(0.01) + # threading.Event().wait(0.01) threading.Event().wait(0.01) # WE NEED TO OPTIMIZE THIS! diff --git a/tnc/rigctld.py b/tnc/rigctld.py index 3e4d2e46..76308fe5 100644 --- a/tnc/rigctld.py +++ b/tnc/rigctld.py @@ -7,6 +7,7 @@ import socket import time import structlog +import threading # set global hamlib version hamlib_version = 0 @@ -133,7 +134,7 @@ class radio: else: # reconnecting.... - time.sleep(0.5) + threading.Event().wait(0.5) self.connect() return b"" diff --git a/tnc/sock.py b/tnc/sock.py index 02e450d5..4fa98ff0 100644 --- a/tnc/sock.py +++ b/tnc/sock.py @@ -76,7 +76,7 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler): if data != tempdata: tempdata = data SOCKET_QUEUE.put(data) - time.sleep(0.5) + threading.Event().wait(0.5) while not SOCKET_QUEUE.empty(): data = SOCKET_QUEUE.get() @@ -99,7 +99,7 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler): # we want to display INFO messages only once static.INFO = [] # self.request.sendall(sock_data) - time.sleep(0.15) + threading.Event().wait(0.15) def receive_from_client(self): """ @@ -134,7 +134,7 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler): # we might improve this by only processing one command or # doing some kind of selection to determin which commands need to be dropped # and which one can be processed during a running transmission - time.sleep(3) + threading.Event().wait(3) # finally delete our rx buffer to be ready for new commands data = bytes() @@ -171,7 +171,7 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler): # keep connection alive until we close it while self.connection_alive and not CLOSE_SIGNAL: - time.sleep(1) + threading.Event().wait(1) def finish(self): """ """