Last active
December 3, 2024 14:38
-
-
Save vndee/8a3ca0a55fef7d53594fc9705964af08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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