Skip to content

Instantly share code, notes, and snippets.

@vndee
Last active December 3, 2024 14:38
Show Gist options
  • Save vndee/8a3ca0a55fef7d53594fc9705964af08 to your computer and use it in GitHub Desktop.
Save vndee/8a3ca0a55fef7d53594fc9705964af08 to your computer and use it in GitHub Desktop.
async def start(self):
server = await asyncio.start_server(
self.handle_client,
self.host,
self.port
)
async with server:
await server.serve_forever()
async def handle_client(self, reader: asyncio.StreamReader,
writer: asyncio.StreamWriter):
try:
while True:
# This 'await' doesn't block other clients
# It's like quickly checking if a table needs attention
data = await reader.read(1024)
if not data:
break
# Process commands asynchronously
commands = self.resp_parser.parse(data)
for cmd in commands:
# Handle each command without blocking other clients
await self.handle_command(reader, writer, cmd)
finally:
writer.close()
await writer.wait_closed()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment