从未等待过协程'AsyncSocketClient.connect'的问题龙卷风(Socket)

时间:2019-04-11 12:30:21

标签: python sockets asynchronous async-await tornado

我正在建立套接字连接(Tornado Web框架。)

我的代码:

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))

运行main.py时,出现以下错误:

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

我尝试使用run_sync()方法,但无法获得结果。我已经通过run_sync()中的lambda了,我能够连接,但是执行以下命令后什么也没做:

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

1 个答案:

答案 0 :(得分:1)

由于connect是一个协程,因此您需要await。为此,您还必须将main函数转换为协程。

但这似乎是多余的,因为您可以使用run_sync达到类似的效果:

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

顺便说一句,如果您要实现Websocket客户端,请知道tornado already comes with one