From 32b5c1ec38a26341b5264ca7c3cd7288a6e76721 Mon Sep 17 00:00:00 2001 From: Wiktor Latanowicz Date: Thu, 23 Mar 2023 21:25:58 +0100 Subject: [PATCH] Allow coroutines as client event callbacks --- docker-examples/client/simple_events/client.py | 2 +- indi/client/client.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docker-examples/client/simple_events/client.py b/docker-examples/client/simple_events/client.py index e747295..4949435 100644 --- a/docker-examples/client/simple_events/client.py +++ b/docker-examples/client/simple_events/client.py @@ -44,7 +44,7 @@ async def main_loop(): control_connection = TCP(host, port) blob_connection = TCP(host, port) - def client_callback(event): + async def client_callback(event): print(event) client = Client(control_connection, blob_connection) diff --git a/indi/client/client.py b/indi/client/client.py index d2e38df..05448b9 100644 --- a/indi/client/client.py +++ b/indi/client/client.py @@ -302,7 +302,10 @@ def trigger_event(self, event: events.BaseEvent): for callback in self.callbacks: if callback.accepts_event(event): try: - callback.callback(event) + if asyncio.iscoroutinefunction(callback.callback): + asyncio.get_running_loop().create_task(callback.callback(event)) + else: + callback.callback(event) except: logger.exception("Error in event handler")