How can I refactor to accept multiple clients?

2019-07-23 20:05发布

I don't understand why server.py Version 1 allows a client to be keyboard-interrupted and restarted, while server.py Version 2 doesn't:

server.py Version1:

import asyncio

async def handle_client(reader, writer):
    while True:
        request = (await reader.read(128)).decode()
        writer.write('Received ok.'.encode())
        await writer.drain()

async def main():
    loop.create_task(asyncio.start_server(handle_client, 'localhost', 15555))

loop = asyncio.new_event_loop()
loop.create_task(main())
loop.run_forever()

server.py Version 2:

import asyncio

async def handle_client(reader, writer):
    while True:
        request = (await reader.read(128)).decode()
        if request == "hello":
            writer.write('Received ok.'.encode())
            await writer.drain()

async def main():
    loop.create_task(asyncio.start_server(handle_client, 'localhost', 15555))

loop = asyncio.new_event_loop()
loop.create_task(main())
loop.run_forever()

client.py:

import asyncio

async def make_connections():
    reader, writer = await asyncio.open_connection('localhost', 15555, loop=loop)
    loop.create_task(connect(reader, writer))


async def connect(reader, writer):
    writer.write("hello".encode())
    await writer.drain()
    result = await reader.read(128)
    print(result.decode())


loop = asyncio.new_event_loop()
loop.create_task(make_connections())
loop.run_forever()

Version 2 works fine for a single client, but if I send a keyboard interrupt to the client I can no longer connect after I restart the client. It's annoying to ssh in and kill/restart the server every time I alter code in the client. I don't see why the second version doesn't accept the client the second time it attempts to connect.

1条回答
不美不萌又怎样
2楼-- · 2019-07-23 20:59

I don't understand why server.py Version 1 allows a client to be keyboard-interrupted and restarted, while server.py Version 2 doesn't

Both versions have a bug that they don't correctly check for the end-of-file condition. When you interrupt the client, the socket gets closed and reading from it returns EOF, while writing to it raises an exception. Awaiting writer.drain() in version 1 delivers the exception and interrupts the coroutine. (This exception is probably displayed on the server's standard error.)

Version 2 has a problem, though: the if request == "hello" test is false at EOF because reader.read() keeps returning an empty byte string to mark the EOF condition. This prevents await writer.drain() from executing and delivering the exception, so the coroutine remains stuck in an infinite loop. A simple fix is to add something like if not request: break after the read.

Why version 2 gets stuck

But the above doesn't fully explain why in Version 2 the whole server is broken and new clients unable to connect. Surely one would expect await to either return a result or yield control to other coroutines. But the observed behavior is that, despite containing an await in the while loop, the coroutine doesn't allow other coroutines to run!

The problem is that await doesn't mean "pass control to the event loop", as it is often understood. It means "request value from the provided awaitable object, yielding control to the event loop if the object indicates that it does not have a value ready." The part after the if is crucial: if the object does have a value ready, this value will be used immediately without ever deferring to the event loop. In other words, await doesn't guarantee that the event loop will get a chance to run.

A stream at EOF always has data to return - the empty string that marks the EOF. As a result, it never gets suspended and the loop ends up completely blocking the event loop. To guarantee that other tasks get a chance to run, you can add await asyncio.sleep(0) in a loop - but this should not be necessary in correctly written code, where requesting IO data will soon result in a wait, at which point the event loop will kick in. Once the EOF handling bug is corrected, the server will function correctly.

查看更多
登录 后发表回答