Search code examples
pythonsocketsasynchronousasync-awaittornado

Facing issue coroutine 'AsyncSocketClient.connect' was never awaited Tornado ( Socket )


I am working on establishing a socket connection ( Tornado Web framework. )

My Code:

main.py

def main():
    io_loop = tornado.ioloop.IOLoop.instance()
    decoder = AsyncSocketClient(host = "localhost", port = 8080, try_reconnect=True , io_loop= io_loop)
    decoder.connect()
    io_loop.start()

if __name__ == '__main__':
    main()

AsyncSocketClient.py

class AsyncSocketClient():
    def __init__(self, host,io_loop , port, try_reconnect=False):
        self.ws_host = host
        self.ws_port = port
        self.io_loop = io_loop

    async def connect(self):
        class AsyncSocket(socket.socket):
            def write_message(self, message):
                message = message.encode()
                self.send(message)
        try:
            self._ws_connection = AsyncSocket(socket.AF_INET, socket.SOCK_STREAM, 0)
            self._ws_connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            await self._ws_connection.connect((self.ws_host, self.ws_port))
            self._ws_connection.setblocking(0)
            self.add_to_ioloop(self.io_loop)
            self._on_connection_success()
        except Exception as e:
            time.sleep(1)
            self.close()

    def add_to_ioloop(self, io_loop):
        io_loop.add_handler(self._ws_connection.fileno(), self._handle_events, io_loop.ERROR)
        io_loop.update_handler(self._ws_connection.fileno(), io_loop.READ)

    async def close(self):
        if not self._ws_connection:
            raise RuntimeError('Web socket connection is already closed.')

        await self._ws_connection.close()
        self._ws_connection = None
        self._on_connection_close()

    async def _on_connection_close(self):
        print("Connection Closed from " + self.ws_host + ":" + str(self.ws_port))
        if self.try_reconnect:
            print("Retrying to connect " + self.ws_host + ":" + str(self.ws_port))
            self.connect()

    def _on_connection_success(self):
        print("Connected to " + self.ws_host + ":" + str(self.ws_port))

When I run main.py, I get the following error:

main.py: RuntimeWarning: coroutine 'AsyncSocketClient.connect' was never awaited
  decoder.connect()

I have tried using run_sync() method But I was unable to achieve the result. I have passed the lambda in run_sync() and I was able to connect But then nothing after this line executed:

await self._ws_connection.connect((self.ws_host, self.ws_port))


Solution

  • Since connect is a coroutine, you need to await it. And for that you'll also have to convert main function to a coroutine.

    But that just seems redundant because you can achieve the similar effect using run_sync:

    if __name__ == '__main__':
        decoder = AsyncSocketClient(...)
        io_loop = tornado.ioloop.IOLoop.current()
        io_loop.run_sync(decoder.connect)
    

    By the way, if you're trying to implement a websocket client, know that tornado already comes with one.