与不可靠服务器的Python异步IO通信

与不可靠服务器的Python异步IO通信,python,python-3.5,python-3.6,python-asyncio,Python,Python 3.5,Python 3.6,Python Asyncio,是否有使用asyncio轮询间歇服务器的标准模式? 以下面的示例为例:每秒从不可靠的TCP服务器读取数据的客户端。如果没有错误处理,async/await符号看起来很棒: class BasicTCPClient(object): """TCP client without error handling.""" def __init__(self, ip, port): self.ip = ip self.port = port asyn

是否有使用asyncio轮询间歇服务器的标准模式?

以下面的示例为例:每秒从不可靠的TCP服务器读取数据的客户端。如果没有错误处理,
async/await
符号看起来很棒:

class BasicTCPClient(object):
    """TCP client without error handling."""
    def __init__(self, ip, port):
        self.ip = ip
        self.port = port

    async def connect(self):
        reader, writer = await asyncio.open_connection(self.ip, self.port)
        self.connection = {'reader': reader, 'writer': writer}

    async def get(self, request):
        self.connection['writer'].write(request.encode())
        return await self.connection['reader'].readuntil(b'\n')


async def read():
    client = BasicTCPClient('ip', 8000)
    await client.connect()
    while True:
        print(await client.get('request'))
        await asyncio.sleep(1)

    ioloop = asyncio.get_event_loop()
    try:
        ioloop.run_until_complete(read())
    except KeyboardInterrupt:
        pass

但这不能很好地处理断开/重新连接。我有一个可行的解决方案,但它接近100 LOC,否定了
async/await

QQ的所有优雅之处:出于任何原因,你反对使用
aiohttp
包吗?服务器没有实现HTTP-它基本上是TCP上的串行。我看到的所有关于
aiohttp
的示例都假设您可以获取并发布.Hmm…好吧,因此aiohttp有一系列较低级别(也称为套接字级别TCP)和可插入部分,但在快速查看之后,我发现的所有部分中似乎都引用了HTTP。一旦我有时间查看这些对象/方法中的一些是否可以在连接实例化前端被篡改,我将尝试发布一个答案。在国际比赛中,也许你能比我做得更好:-.&-