I am using my Raspberry Pi and the pigpio and websockets libraries.
I want my program to run asynchronously (i.e. I will use async def main as the entry point).
The pigpio library expects a synchronous callback function to be called in response to events, which is fine, but from within that callback I want to call another, asynchronous function from the websocket library.
So it would look like:
def sync_cb(): # <- This can not be made async, therefore I can not use await
[ws.send('test') for ws in connected_ws] # <- This is async and has to be awaited
Currently I can get it to work with:
def sync_cb():
asyncio.run(asyncio.wait([ws.send('test') for ws in connected_ws]))
but the docs say this use of asyncio.run is discouraged.
So my synchronous callback needs to call ws.send (also from a third party library) which is async from a function that is synchronous.
Another option that works is:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(asyncio.gather(*[ws.send(json.dumps(message)) for ws in connected_ws]))
But the three lines of creating and setting an even loop sounds like a lot just to run a simple async function.
My questions are:
- Is it possible to substitute an async function where a synchronous callback is required (i.e. is there a way to make
cbasync in this example) - And, what kind of overhead am I incurring by using
asyncio.runandasyncio.waitjust to call a simple async method (in the list comprehension)