Browse Source

fix async issue

tuanchris 4 months ago
parent
commit
ed6f63a051
2 changed files with 30 additions and 9 deletions
  1. 28 7
      modules/connection/connection_manager.py
  2. 2 2
      modules/core/pattern_manager.py

+ 28 - 7
modules/connection/connection_manager.py

@@ -479,23 +479,44 @@ def home(timeout=15):
 
 def check_idle():
     """
-    Continuously check if the device is idle.
+    Continuously check if the device is idle (synchronous version).
     """
     logger.info("Checking idle")
     while True:
         response = get_status_response()
         if response and "Idle" in response:
             logger.info("Device is idle")
-            # Run async update_machine_position in sync context
+            # Schedule async update_machine_position in the existing event loop
             try:
-                loop = asyncio.new_event_loop()
-                asyncio.set_event_loop(loop)
-                loop.run_until_complete(update_machine_position())
-                loop.close()
+                # Try to schedule in existing event loop if available
+                try:
+                    loop = asyncio.get_running_loop()
+                    # Create a task but don't await it (fire and forget)
+                    asyncio.create_task(update_machine_position())
+                    logger.debug("Scheduled machine position update task")
+                except RuntimeError:
+                    # No event loop running, skip machine position update
+                    logger.debug("No event loop running, skipping machine position update")
             except Exception as e:
-                logger.error(f"Error updating machine position: {e}")
+                logger.error(f"Error scheduling machine position update: {e}")
             return True
         time.sleep(1)
+
+async def check_idle_async():
+    """
+    Continuously check if the device is idle (async version).
+    """
+    logger.info("Checking idle (async)")
+    while True:
+        response = await asyncio.to_thread(get_status_response)
+        if response and "Idle" in response:
+            logger.info("Device is idle")
+            try:
+                await update_machine_position()
+            except Exception as e:
+                logger.error(f"Error updating machine position: {e}")
+            return True
+        await asyncio.sleep(1)
         
 
 def get_machine_position(timeout=5):

+ 2 - 2
modules/core/pattern_manager.py

@@ -525,7 +525,7 @@ async def run_theta_rho_file(file_path, is_playlist=False):
                 
                 if state.skip_requested:
                     logger.info("Skipping pattern...")
-                    connection_manager.check_idle()
+                    await connection_manager.check_idle_async()
                     if state.led_controller:
                         effect_idle(state.led_controller)
                     break
@@ -568,7 +568,7 @@ async def run_theta_rho_file(file_path, is_playlist=False):
             logger.error("Device is not connected. Stopping pattern execution.")
             return
             
-        connection_manager.check_idle()
+        await connection_manager.check_idle_async()
         
         # Set LED back to idle when pattern completes normally (not stopped early)
         if state.led_controller and not state.stop_requested: