Pythonic way to process a synchronous event (callback) in an async environment

83 views Asked by At

I'm looking for a solution with which I could use the Python socketIO disconnect event and forward this into a fully asynchronous host class.

    @self._sio.event
    def disconnect():
        ''' Platform disconnected '''
        logging.info("disconnected")
        self._delegate.signaling_client_left()

self._delegate.signaling_client_left() points to a non-async function. How could I make this an async function? I tried with asyncio.run(), but this leads to asyncio.run() cannot be called from a running event loop

1

There are 1 answers

1
decades On

This seems to work.

@self._sio.event
        def disconnect():
            ''' Platform disconnected '''
            logging.info("disconnected")
            
            async def fire():
                self._delegate.signaling_client_left()
            asyncio.create_task(fire())