sde1000 / python-dali

Library for controlling DALI lighting systems
Other
150 stars 73 forks source link

async callbacks? #82

Closed jschlyter closed 3 years ago

jschlyter commented 3 years ago

Is there any way to create async callbacks, e.g., that the function provided to register() is async? Or is callbacks always sync?

cc @fredriklj

sde1000 commented 3 years ago

Are you talking about the code in dali.drivers.hid? If so, callbacks are made using the call_soon() method of the event loop. If you want to execute async code from the callback, you should be able to use create_task() in the normal way.

(Arguably the code that makes the callback ought to be able to spot that the target is a coroutine and call create_task() itself; I'll consider that for the future.)

jschlyter commented 3 years ago

So something like this should work?

def callback(*args, **kwargs):
    await asyncio.create_task(async_callback(*args, **kwargs))
sde1000 commented 3 years ago

Except without the "await": you're creating a new task, not waiting for a coroutine to complete.

jschlyter commented 3 years ago

Works, thanks a lot!