mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-06-11 11:56:18 +00:00
Fix event loop deadlock issue
This commit is contained in:
@@ -4,7 +4,7 @@ import asyncio
|
|||||||
from meshcore import MeshCore
|
from meshcore import MeshCore
|
||||||
from meshcore import BLEConnection
|
from meshcore import BLEConnection
|
||||||
|
|
||||||
ADDRESS = "T1000_S" # node ble adress or name
|
ADDRESS = "Meshcore-lora-py-tester" # node ble adress or name
|
||||||
|
|
||||||
async def main () :
|
async def main () :
|
||||||
con = BLEConnection(ADDRESS)
|
con = BLEConnection(ADDRESS)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
from math import log
|
from math import log
|
||||||
from typing import Any, Dict, Optional, Callable, List, Union
|
from typing import Any, Dict, Optional, Callable, List, Union
|
||||||
@@ -133,6 +134,7 @@ class EventDispatcher:
|
|||||||
while self.running:
|
while self.running:
|
||||||
event = await self.queue.get()
|
event = await self.queue.get()
|
||||||
logger.debug(f"Dispatching event: {event.type}, {event.payload}, {event.attributes}")
|
logger.debug(f"Dispatching event: {event.type}, {event.payload}, {event.attributes}")
|
||||||
|
|
||||||
for subscription in self.subscriptions.copy():
|
for subscription in self.subscriptions.copy():
|
||||||
# Check if event type matches
|
# Check if event type matches
|
||||||
if subscription.event_type is None or subscription.event_type == event.type:
|
if subscription.event_type is None or subscription.event_type == event.type:
|
||||||
@@ -142,15 +144,24 @@ class EventDispatcher:
|
|||||||
if not all(event.attributes.get(key) == value
|
if not all(event.attributes.get(key) == value
|
||||||
for key, value in subscription.attribute_filters.items()):
|
for key, value in subscription.attribute_filters.items()):
|
||||||
continue
|
continue
|
||||||
try:
|
|
||||||
result = subscription.callback(event.clone())
|
# Fire and forget - don't await!
|
||||||
if asyncio.iscoroutine(result):
|
asyncio.create_task(self._execute_callback(subscription.callback, event.clone()))
|
||||||
await result
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error in event handler: {e}")
|
|
||||||
|
|
||||||
self.queue.task_done()
|
self.queue.task_done()
|
||||||
|
|
||||||
|
async def _execute_callback(self, callback, event):
|
||||||
|
"""Execute a callback with proper error handling"""
|
||||||
|
try:
|
||||||
|
if asyncio.iscoroutinefunction(callback):
|
||||||
|
await callback(event)
|
||||||
|
else:
|
||||||
|
result = callback(event)
|
||||||
|
if inspect.iscoroutine(result):
|
||||||
|
await result
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in event handler for {event.type}: {e}", exc_info=True)
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
if not self.running:
|
if not self.running:
|
||||||
self.running = True
|
self.running = True
|
||||||
|
|||||||
Reference in New Issue
Block a user