Hotfix : mavlink_object 內 process_data 的 try 迴圈刪減

(try 嚴重影響效能)
chiyu
Chiyu Chen 11 months ago
parent 6fab72ac97
commit 2c4fc4583e

@ -331,18 +331,18 @@ class mavlink_object:
try:
# 處理接收到的封包
mavlinkMsg = self.mavlink_socket.recv_msg()
except Exception as e:
logger.warning(f"Error in mavlink_object.process_data for id {self.socket_id}: {e}")
self.dirtyDataCount += 1
# except Exception as e:
# logger.warning(f"Error in mavlink_object.process_data for id {self.socket_id}: {e}")
# self.dirtyDataCount += 1
if self.dirtyDataCount >= self.dirtyDataMax:
logger.error(f"Too many dirty data received in mavlink_object {self.socket_id}, entering ERROR state")
self.state = MavlinkObjectState.ERROR
# 不直接退出,嘗試容忍髒數據
await asyncio.sleep(0.01) # 短暫暫停
continue
# if self.dirtyDataCount >= self.dirtyDataMax:
# logger.error(f"Too many dirty data received in mavlink_object {self.socket_id}, entering ERROR state")
# self.state = MavlinkObjectState.ERROR
# # 不直接退出,嘗試容忍髒數據
# await asyncio.sleep(0.01) # 短暫暫停
# continue
try:
# try:
if mavlinkMsg:
# 統計收到的訊息 & 透過 mavlink 封包的序列號來檢查是否有遺失的封包 & 記錄最後收到的封包時間
sysid = mavlinkMsg.get_srcSystem()
@ -374,7 +374,7 @@ class mavlink_object:
logger.error(f"Error forwarding message to port {target_port}: {e}")
with self.outgoing_msg_lock:
if self.outgoing_messages and (send_msg := self.outgoing_messages.pop(0)):
if self.outgoing_msgs and (send_msg := self.outgoing_msgs.pop(0)):
try:
self.mavlink_socket.write(send_msg)
except Exception as e:
@ -387,6 +387,13 @@ class mavlink_object:
logger.info(f'mavlink_object.process_data for id {self.socket_id} was cancelled')
break
except Exception as e:
logger.warning(f"Error in mavlink_object.process_data for id {self.socket_id}: {e}")
self.dirtyDataCount += 1
if self.dirtyDataCount >= self.dirtyDataMax:
logger.error(f"Too many dirty data received in mavlink_object {self.socket_id}, entering ERROR state")
self.state = MavlinkObjectState.ERROR
# 不直接退出,嘗試容忍髒數據
await asyncio.sleep(0.01) # 短暫暫停避免CPU過載
@ -442,8 +449,8 @@ class mavlink_object:
try:
# 使用鎖保護共享資源訪問
with self.outgoing_messages_lock:
self.outgoing_messages.append(message_bytes)
with self.outgoing_msgs_lock:
self.outgoing_msgs.append(message_bytes)
return True
except Exception as e:
logger.error(f"Error queueing message for mavlink_object {self.socket_id}: {e}")

@ -18,8 +18,8 @@ from fc_network_adapter import mavlinkDevice as md
# ====================== 分割線 =====================
test_item = 21
running_time = 30
test_item = 12
running_time = 10000
print('test_item : ', test_item)
@ -147,6 +147,8 @@ elif test_item == 12:
manager.add_mavlink_object(mavlink_object_in1)
manager.add_mavlink_object(mavlink_object_in2)
time.sleep(1) # 等待通道啟動
mavlink_object_in1.add_target_socket(mavlink_object_out.socket_id)
mavlink_object_out.add_target_socket(mavlink_object_in1.socket_id)

Loading…
Cancel
Save