소스 검색

```
feat(mcu): 将设备控制命令发送改为异步处理

将 DeviceControl.send_all_cmd 方法改为异步方法,并调整相关调用方式,
使用 asyncio.create_task 替代直接同步调用以提升性能。

BREAKING CHANGE: 命令发送机制从同步改为异步执行
```

rambo 1 주 전
부모
커밋
e32e938554
3개의 변경된 파일10개의 추가작업 그리고 9개의 파일을 삭제
  1. 2 1
      python/mcu/DeviceControl.py
  2. 1 1
      python/mcu/Mcu.py
  3. 7 7
      python/mcu/ProgramItem.py

+ 2 - 1
python/mcu/DeviceControl.py

@@ -403,9 +403,10 @@ class DeviceControl(BaseClass, metaclass=SingletonType):
             self.send_data_queue.append(data)
         self.lock.release()
 
-    def send_all_cmd(self):
+    async def send_all_cmd(self):
         # await asyncio.sleep(0.001)
         while True:
+            await asyncio.sleep(0.1)
             if self.send_data_queue:
                 # self.sendSocketMessage(msg="正在发送命令", device_status=1)
                 data = self.send_data_queue.pop(0)

+ 1 - 1
python/mcu/Mcu.py

@@ -655,7 +655,7 @@ class Mcu(BaseClass, metaclass=SingletonType):
         self.to_init_device_origin_point(device_name="mcu")
         print("MCU 开始循环~")
         while 1:
-            time.sleep(0.01)
+            time.sleep(0.1)
             if not self.serial_ins or not self.connect_state:
                 break
             try:

+ 7 - 7
python/mcu/ProgramItem.py

@@ -174,8 +174,8 @@ class ProgramItem(BaseClass):
                 return True
             else:
                 self.mcu.to_get_mcu_base_info()
-                # asyncio.create_task(self.mcu.send_all_cmd())
-                self.mcu.send_all_cmd()
+                asyncio.create_task(self.mcu.send_all_cmd())
+                # self.mcu.send_all_cmd()
                 await asyncio.sleep(0.5)
                 self.mcu.get_basic_info_mcu()
                 # return True
@@ -231,9 +231,9 @@ class ProgramItem(BaseClass):
                     device_name="turntable_steering", value=self.turntable_angle
                 )
                 time.sleep(0.1)
-            # loop = asyncio.get_event_loop()
-            self.mcu.send_all_cmd()
-            # loop.create_task(self.mcu.send_all_cmd())
+            loop = asyncio.get_event_loop()
+            # self.mcu.send_all_cmd()
+            loop.create_task(self.mcu.send_all_cmd())
 
     async def do_run(self, *args):
         await asyncio.sleep(0.001)
@@ -284,8 +284,8 @@ class ProgramItem(BaseClass):
                 # time.sleep(0.1)
 
             # MCU运动是否有停止检查,设定超时时间
-            self.mcu.send_all_cmd()
-            # asyncio.create_task(self.mcu.send_all_cmd())
+            # self.mcu.send_all_cmd()
+            asyncio.create_task(self.mcu.send_all_cmd())
             if self.mode_type != "其他配置":
                 await asyncio.sleep(1.2)
                 print("二次检查")