attempt using threading.Event().wait() instead of time.sleep()

This commit is contained in:
DJ2LS 2022-12-12 12:28:52 +01:00
parent 10b925d70b
commit 6e3edb5b30
8 changed files with 40 additions and 38 deletions

View file

@ -93,7 +93,7 @@ class DAEMON:
"[DMN] update_audio_devices: Exception gathering audio devices:", "[DMN] update_audio_devices: Exception gathering audio devices:",
e=err1, e=err1,
) )
time.sleep(1) threading.Event().wait(1)
def update_serial_devices(self): def update_serial_devices(self):
""" """
@ -114,7 +114,7 @@ class DAEMON:
) )
static.SERIAL_DEVICES = serial_devices static.SERIAL_DEVICES = serial_devices
time.sleep(1) threading.Event().wait(1)
except Exception as err1: except Exception as err1:
self.log.error( self.log.error(
"[DMN] update_serial_devices: Exception gathering serial devices:", "[DMN] update_serial_devices: Exception gathering serial devices:",
@ -443,4 +443,4 @@ if __name__ == "__main__":
version=static.VERSION, version=static.VERSION,
) )
while True: while True:
time.sleep(1) threading.Event().wait(1)

View file

@ -402,7 +402,7 @@ class DATA:
# Wait while transmitting # Wait while transmitting
while static.TRANSMITTING: while static.TRANSMITTING:
time.sleep(0.01) threading.Event().wait(0.01)
def send_data_to_socket_queue(self, **jsondata): 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 # while (not self.burst_ack and not self.burst_nack and
# not self.rpt_request_received and not self.data_frame_ack_received and # not self.rpt_request_received and not self.data_frame_ack_received and
# time.time() < burstacktimeout and static.ARQ_STATE): # 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 # burstacktimeout = time.time() + self.burst_ack_timeout_seconds + 100
while static.ARQ_STATE and not ( while static.ARQ_STATE and not (
@ -1085,7 +1085,7 @@ class DATA:
or self.rpt_request_received or self.rpt_request_received
or self.data_frame_ack_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 # Once we receive a burst ack, reset its state and break the RETRIES loop
if self.burst_ack: if self.burst_ack:
@ -1162,7 +1162,7 @@ class DATA:
# we need to wait until sending "transmitted" state # we need to wait until sending "transmitted" state
# gui database is too slow for handling this within 0.001 seconds # gui database is too slow for handling this within 0.001 seconds
# so let's sleep a little # so let's sleep a little
time.sleep(0.2) threading.Event().wait(0.2)
self.send_data_to_socket_queue( self.send_data_to_socket_queue(
freedata="tnc-message", freedata="tnc-message",
arq="transmission", arq="transmission",
@ -1402,7 +1402,7 @@ class DATA:
# wait while timeout not reached and our busy state is busy # wait while timeout not reached and our busy state is busy
channel_busy_timeout = time.time() + 30 channel_busy_timeout = time.time() + 30
while static.CHANNEL_BUSY and time.time() < channel_busy_timeout: 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 channel busy timeout reached stop connecting
if time.time() > channel_busy_timeout: if time.time() > channel_busy_timeout:
@ -1422,7 +1422,7 @@ class DATA:
# wait until data channel is open # wait until data channel is open
while not static.ARQ_SESSION and not self.arq_session_timeout: 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" static.ARQ_SESSION_STATE = "connecting"
self.send_data_to_socket_queue( self.send_data_to_socket_queue(
freedata="tnc-message", freedata="tnc-message",
@ -1486,7 +1486,7 @@ class DATA:
connection_frame[8:14] = helpers.callsign_to_bytes(self.mycallsign) connection_frame[8:14] = helpers.callsign_to_bytes(self.mycallsign)
while not static.ARQ_SESSION: while not static.ARQ_SESSION:
time.sleep(0.01) threading.Event().wait(0.01)
for attempt in range(self.session_connect_max_retries): for attempt in range(self.session_connect_max_retries):
self.log.info( self.log.info(
"[TNC] SESSION [" "[TNC] SESSION ["
@ -1514,7 +1514,7 @@ class DATA:
# indicates we've received a positive response from the far station. # indicates we've received a positive response from the far station.
timeout = time.time() + 3 timeout = time.time() + 3
while time.time() < timeout: while time.time() < timeout:
time.sleep(0.01) threading.Event().wait(0.01)
# Stop waiting if data channel is opened # Stop waiting if data channel is opened
if static.ARQ_SESSION: if static.ARQ_SESSION:
return True return True
@ -1790,7 +1790,7 @@ class DATA:
# wait a moment for the case, a heartbeat is already on the way back to us # wait a moment for the case, a heartbeat is already on the way back to us
# this makes channel establishment more clean # this makes channel establishment more clean
if static.ARQ_SESSION: if static.ARQ_SESSION:
time.sleep(2) threading.Event().wait(2)
self.datachannel_timeout = False self.datachannel_timeout = False
@ -1813,7 +1813,7 @@ class DATA:
# wait while timeout not reached and our busy state is busy # wait while timeout not reached and our busy state is busy
channel_busy_timeout = time.time() + 30 channel_busy_timeout = time.time() + 30
while static.CHANNEL_BUSY and time.time() < channel_busy_timeout: 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 channel busy timeout reached, stop connecting
if time.time() > channel_busy_timeout: if time.time() > channel_busy_timeout:
@ -1834,7 +1834,7 @@ class DATA:
# wait until data channel is open # wait until data channel is open
while not static.ARQ_STATE and not self.datachannel_timeout: while not static.ARQ_STATE and not self.datachannel_timeout:
time.sleep(0.01) threading.Event().wait(0.01)
if static.ARQ_STATE: if static.ARQ_STATE:
self.arq_transmit(data_out, mode, n_frames_per_burst) self.arq_transmit(data_out, mode, n_frames_per_burst)
@ -1884,7 +1884,7 @@ class DATA:
connection_frame[13:14] = self.session_id connection_frame[13:14] = self.session_id
while not static.ARQ_STATE: while not static.ARQ_STATE:
time.sleep(0.01) threading.Event().wait(0.01)
for attempt in range(self.data_channel_max_retries): for attempt in range(self.data_channel_max_retries):
self.send_data_to_socket_queue( self.send_data_to_socket_queue(
@ -1908,7 +1908,7 @@ class DATA:
timeout = time.time() + 3 timeout = time.time() + 3
while time.time() < timeout: while time.time() < timeout:
time.sleep(0.01) threading.Event().wait(0.01)
# Stop waiting if data channel is opened # Stop waiting if data channel is opened
if static.ARQ_STATE: if static.ARQ_STATE:
return True return True
@ -2406,7 +2406,7 @@ class DATA:
""" """
try: try:
while True: while True:
time.sleep(0.5) threading.Event().wait(0.5)
while static.BEACON_STATE: while static.BEACON_STATE:
if ( if (
not static.ARQ_SESSION not static.ARQ_SESSION
@ -2444,7 +2444,7 @@ class DATA:
and static.BEACON_STATE and static.BEACON_STATE
and not static.BEACON_PAUSE and not static.BEACON_PAUSE
): ):
time.sleep(0.01) threading.Event().wait(0.01)
except Exception as err: except Exception as err:
self.log.debug("[TNC] run_beacon: ", exception=err) self.log.debug("[TNC] run_beacon: ", exception=err)
@ -2845,7 +2845,7 @@ class DATA:
""" """
while True: while True:
time.sleep(0.1) threading.Event().wait(0.1)
self.data_channel_keep_alive_watchdog() self.data_channel_keep_alive_watchdog()
self.burst_watchdog() self.burst_watchdog()
self.arq_session_keep_alive_watchdog() self.arq_session_keep_alive_watchdog()
@ -2919,7 +2919,7 @@ class DATA:
""" """
# and not static.ARQ_SEND_KEEP_ALIVE: # and not static.ARQ_SEND_KEEP_ALIVE:
if static.ARQ_STATE and static.TNC_STATE == "BUSY": if static.ARQ_STATE and static.TNC_STATE == "BUSY":
time.sleep(0.01) threading.Event().wait(0.01)
if ( if (
self.data_channel_last_received + self.transmission_timeout self.data_channel_last_received + self.transmission_timeout
> time.time() > time.time()
@ -2927,7 +2927,7 @@ class DATA:
timeleft = (self.data_channel_last_received + self.transmission_timeout) - time.time() timeleft = (self.data_channel_last_received + self.transmission_timeout) - time.time()
self.log.debug("Time left until timeout", seconds=timeleft) 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()) # print(self.data_channel_last_received + self.transmission_timeout - time.time())
# pass # pass
else: else:
@ -2961,7 +2961,7 @@ class DATA:
and not self.arq_file_transfer and not self.arq_file_transfer
): ):
if self.arq_session_last_received + self.arq_session_timeout > time.time(): if self.arq_session_last_received + self.arq_session_timeout > time.time():
time.sleep(0.01) threading.Event().wait(0.01)
else: else:
self.log.info( self.log.info(
"[TNC] SESSION [" "[TNC] SESSION ["
@ -2985,19 +2985,19 @@ class DATA:
Heartbeat thread which auto pauses and resumes the heartbeat signal when in an arq session Heartbeat thread which auto pauses and resumes the heartbeat signal when in an arq session
""" """
while True: while True:
time.sleep(0.01) threading.Event().wait(0.01)
# additional check for smoother stopping if heartbeat transmission # additional check for smoother stopping if heartbeat transmission
while not self.arq_file_transfer: while not self.arq_file_transfer:
time.sleep(0.01) threading.Event().wait(0.01)
if ( if (
static.ARQ_SESSION static.ARQ_SESSION
and self.IS_ARQ_SESSION_MASTER and self.IS_ARQ_SESSION_MASTER
and static.ARQ_SESSION_STATE == "connected" and static.ARQ_SESSION_STATE == "connected"
# and not self.arq_file_transfer # and not self.arq_file_transfer
): ):
time.sleep(1) threading.Event().wait(1)
self.transmit_session_heartbeat() self.transmit_session_heartbeat()
time.sleep(2) threading.Event().wait(2)
def send_test_frame(self) -> None: def send_test_frame(self) -> None:
"""Send an empty test frame""" """Send an empty test frame"""

View file

@ -28,7 +28,7 @@ class explorer():
def interval(self): def interval(self):
while True: while True:
self.push() self.push()
time.sleep(self.publish_interval) threading.Event().wait(self.publish_interval)
def push(self): def push(self):

View file

@ -10,6 +10,7 @@ import crcengine
import static import static
import structlog import structlog
import numpy as np import numpy as np
import threading
log = structlog.get_logger("helpers") log = structlog.get_logger("helpers")
@ -25,7 +26,7 @@ def wait(seconds: float) -> bool:
timeout = time.time() + seconds timeout = time.time() + seconds
while time.time() < timeout: while time.time() < timeout:
time.sleep(0.01) threading.Event().wait(0.01)
return True return True

View file

@ -437,4 +437,4 @@ if __name__ == "__main__":
log.error("[TNC] Starting TCP/IP socket failed", port=static.PORT, e=err) log.error("[TNC] Starting TCP/IP socket failed", port=static.PORT, e=err)
sys.exit(1) sys.exit(1)
while True: while True:
time.sleep(1) threading.Event().wait(1)

View file

@ -291,7 +291,7 @@ class RF:
depositing the data into the codec data buffers. depositing the data into the codec data buffers.
""" """
while True: while True:
time.sleep(0.01) threading.Event().wait(0.01)
# -----read # -----read
data_in48k = bytes() data_in48k = bytes()
with open(RXCHANNEL, "rb") as fifo: with open(RXCHANNEL, "rb") as fifo:
@ -322,7 +322,7 @@ class RF:
def mkfifo_write_callback(self) -> None: def mkfifo_write_callback(self) -> None:
"""Support testing by writing the audio data to a pipe.""" """Support testing by writing the audio data to a pipe."""
while True: while True:
time.sleep(0.01) threading.Event().wait(0.01)
# -----write # -----write
if len(self.modoutqueue) <= 0 or self.mod_out_locked: if len(self.modoutqueue) <= 0 or self.mod_out_locked:
@ -529,7 +529,7 @@ class RF:
self.mod_out_locked = False self.mod_out_locked = False
while self.modoutqueue: while self.modoutqueue:
time.sleep(0.01) threading.Event().wait(0.01)
static.PTT_STATE = self.hamlib.set_ptt(False) static.PTT_STATE = self.hamlib.set_ptt(False)
@ -887,7 +887,7 @@ class RF:
rms_counter = 0 rms_counter = 0
while True: while True:
# time.sleep(0.01) # threading.Event().wait(0.01)
threading.Event().wait(0.01) threading.Event().wait(0.01)
# WE NEED TO OPTIMIZE THIS! # WE NEED TO OPTIMIZE THIS!

View file

@ -7,6 +7,7 @@
import socket import socket
import time import time
import structlog import structlog
import threading
# set global hamlib version # set global hamlib version
hamlib_version = 0 hamlib_version = 0
@ -133,7 +134,7 @@ class radio:
else: else:
# reconnecting.... # reconnecting....
time.sleep(0.5) threading.Event().wait(0.5)
self.connect() self.connect()
return b"" return b""

View file

@ -76,7 +76,7 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
if data != tempdata: if data != tempdata:
tempdata = data tempdata = data
SOCKET_QUEUE.put(data) SOCKET_QUEUE.put(data)
time.sleep(0.5) threading.Event().wait(0.5)
while not SOCKET_QUEUE.empty(): while not SOCKET_QUEUE.empty():
data = SOCKET_QUEUE.get() data = SOCKET_QUEUE.get()
@ -99,7 +99,7 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
# we want to display INFO messages only once # we want to display INFO messages only once
static.INFO = [] static.INFO = []
# self.request.sendall(sock_data) # self.request.sendall(sock_data)
time.sleep(0.15) threading.Event().wait(0.15)
def receive_from_client(self): def receive_from_client(self):
""" """
@ -134,7 +134,7 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
# we might improve this by only processing one command or # we might improve this by only processing one command or
# doing some kind of selection to determin which commands need to be dropped # doing some kind of selection to determin which commands need to be dropped
# and which one can be processed during a running transmission # 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 # finally delete our rx buffer to be ready for new commands
data = bytes() data = bytes()
@ -171,7 +171,7 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
# keep connection alive until we close it # keep connection alive until we close it
while self.connection_alive and not CLOSE_SIGNAL: while self.connection_alive and not CLOSE_SIGNAL:
time.sleep(1) threading.Event().wait(1)
def finish(self): def finish(self):
""" """ """ """