Skip to content

Commit

Permalink
Merge pull request #12 from wlatanowicz/wl/async-client
Browse files Browse the repository at this point in the history
Allow coroutines as client event callbacks
  • Loading branch information
wlatanowicz authored Mar 23, 2023
2 parents 1b0ad22 + 32b5c1e commit 5521f01
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docker-examples/client/simple_events/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion indi/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 5521f01

Please sign in to comment.