Skip to content

Commit 0b73661

Browse files
Tom AlmogTom Almog
authored andcommitted
removed inner loop
1 parent c1d2ada commit 0b73661

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

bootcamp_main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,17 @@ def main() -> int:
192192
current_state = "Unknown"
193193
while time.time() < end_time:
194194
try:
195+
# Heartbeat state
195196
state = hb_recv_to_main_queue.queue.get(timeout=HEARTBEAT_PERIOD_S * 2)
196197
current_state = str(state)
197198
main_logger.info(f"Heartbeat state: {current_state}")
198199
if current_state == "Disconnected":
199200
break
200-
except: # pylint: disable=bare-except
201-
pass
202-
# Also read any command outputs
203-
try:
204-
cmd_out = command_to_main_queue.queue.get(timeout=0.1)
205-
if cmd_out is not None:
201+
# Drain any command outputs without blocking
202+
while True:
203+
cmd_out = command_to_main_queue.queue.get_nowait()
204+
if cmd_out is None:
205+
continue
206206
main_logger.info(f"Command: {cmd_out}")
207207
except: # pylint: disable=bare-except
208208
pass

modules/heartbeat/heartbeat_receiver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def create(
2222
cls,
2323
connection: mavutil.mavfile,
2424
heartbeat_period_s: float,
25+
disconnect_threshold: int,
2526
local_logger: logger.Logger,
2627
) -> "tuple[bool, HeartbeatReceiver | None]":
2728
"""
@@ -32,6 +33,7 @@ def create(
3233
HeartbeatReceiver.__private_key,
3334
connection,
3435
heartbeat_period_s,
36+
disconnect_threshold,
3537
)
3638
except: # pylint: disable=bare-except
3739
local_logger.error("Failed to create HeartbeatReceiver", True)
@@ -42,17 +44,18 @@ def __init__(
4244
key: object,
4345
connection: mavutil.mavfile,
4446
heartbeat_period_s: float,
47+
disconnect_threshold: int,
4548
) -> None:
4649
assert key is HeartbeatReceiver.__private_key, "Use create() method"
4750

4851
# Do any intializiation here
4952
self.__connection = connection
5053
self.__period_s = heartbeat_period_s
5154
self.__missed_in_row = 0
55+
self.__disconnect_threshold = disconnect_threshold
5256

5357
def run(
5458
self,
55-
disconnect_threshold: int,
5659
local_logger: logger.Logger,
5760
) -> "tuple[bool, str]":
5861
"""
@@ -74,7 +77,9 @@ def run(
7477
else:
7578
self.__missed_in_row = 0
7679

77-
state = "Connected" if self.__missed_in_row < disconnect_threshold else "Disconnected"
80+
state = (
81+
"Connected" if self.__missed_in_row < self.__disconnect_threshold else "Disconnected"
82+
)
7883
local_logger.info(f"State: {state}", True)
7984
return True, state
8085

modules/heartbeat/heartbeat_receiver_worker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def heartbeat_receiver_worker(
5252
ok, instance = heartbeat_receiver.HeartbeatReceiver.create(
5353
connection,
5454
heartbeat_period_s,
55+
disconnect_threshold,
5556
local_logger,
5657
)
5758
if not ok:
@@ -63,7 +64,7 @@ def heartbeat_receiver_worker(
6364
while not controller.is_exit_requested():
6465
controller.check_pause()
6566
try:
66-
_ok, state = instance.run(disconnect_threshold, local_logger)
67+
_ok, state = instance.run(local_logger)
6768
except Exception as e: # pylint: disable=broad-except
6869
local_logger.error(f"Heartbeat receive failed: {e}", True)
6970
state = "Disconnected"

0 commit comments

Comments
 (0)