python - Pythonic asyncio communication with unreliable servers -
is there standard pattern using asyncio poll intermittent servers?
take following example: client reads every second unreliable tcp server. without error handling, async/await
notation looks great:
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
but doesn't handle disconnecting/reconnecting well. have working solution, it's approaching 100 loc , negates elegance of async/await
.
Comments
Post a Comment