From 37bcfd59511829d1a01924b68991b2def87d1b92 Mon Sep 17 00:00:00 2001 From: KubaPro010 Date: Tue, 24 Mar 2026 22:09:47 +0100 Subject: [PATCH] improve performance --- xrd.py | 48 ++++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/xrd.py b/xrd.py index 4942cc9..af7e9b6 100644 --- a/xrd.py +++ b/xrd.py @@ -11,6 +11,7 @@ from functools import wraps from typing import Callable from dataclasses import dataclass from enum import IntEnum +import select class Modulation(IntEnum): FM = 0 @@ -90,12 +91,6 @@ def init_tef(): tef.FM_Set_ChannelEqualizer(INITIAL_EQ) tef.FM_Set_MphSuppression(INITIAL_IMS) - tef.FM_Set_LevelStep(-1, -1, -1, -1, -4, -8, 0) - tef.FM_Set_Highcut_Mph(0) - tef.FM_Set_Highcut_Max(False) - tef.FM_Set_Lowcut_Max(True, 60) - tef.FM_Set_Stereo_Time(60, 120, 100, 200) - tef.FM_Set_Stereo_Max(STEREO_BLENDING) tef.APPL_Set_OperationMode(True) # Turn off return tef @@ -216,9 +211,8 @@ def reset_periodic(): def run_periodic(*args, **kwargs): for (func, t, timer) in PERIODIC_FUNCTIONS: - max_catchup = 10 count = 0 - while timer.get_time() > t and count < max_catchup: + while timer.get_time() > t and count < 5: func(*args, **kwargs) timer.subtract(t) count += 1 @@ -321,7 +315,6 @@ def run_server(): if not authenticate(conn): print("Authentication failed.") continue - conn.setblocking(False) # Send initial state conn.sendall(f"T{state.last_tune_fm*10}\n".encode()) @@ -331,27 +324,26 @@ def run_server(): conn.sendall(f"W{state.bw_fm}\n".encode()) conn.sendall(f"M{tef.modulation}\n".encode()) - while True: + while True: + readable, _, _ = select.select([conn], [], [], 0.03) + if readable: + data = conn.recv(256) + if not data: break try: - if not (data := conn.recv(1024)): break + resp = process_command(tef, data, state, conn) + if resp: conn.sendall(resp) + except Exception as e: try: - resp = process_command(tef, data, state, conn) - if resp: conn.sendall(resp) - except Exception as e: - try: - print(e) - conn.sendall(b"!" + str(e).encode() + b"\n") - except Exception: pass - except ConnectionResetError: break - except ConnectionAbortedError: break - except BlockingIOError: - try: run_periodic(tef, conn, state) - except Exception as e: - try: - print(e) - conn.sendall(b"!" + str(e).encode() + b"\n") - except Exception: pass - time.sleep(0.03) + print(e) + conn.sendall(b"!" + str(e).encode() + b"\n") + except Exception: pass + else: + try: run_periodic(tef, conn, state) + except Exception as e: + try: + print(e) + conn.sendall(b"!" + str(e).encode() + b"\n") + except Exception: pass import traceback if __name__ == "__main__":