I’m using asyncio to send data to server. I find that if data is very large and server is busy so that data will keep in buffer, then “await writer.wait_closed()” doen’t close socket, socket will remain alive and buffer is not relieved as long as network is well connected(I’ve waited for at least ten minutes). Only after server is closing or I manually interrupt the connection then client OS will close socket(I learn that from windows resource manager). Why? I think “wait_close” ought to actually close socket and clean buffer no matter what because the python doc says “Wait until the stream is closed. Should be called after
close() to wait until the underlying connection is closed, ensuring that all data has been flushed before e.g. exiting the program.”
Here is my code. Thanks for your kindly help.
Maybe I was wrong, closing connection doesn’t mean transportation is stopped, OS will still send buffer data until buffer is empty, and this process is not controlled by python, is it?
# Server import asyncio async def handle(reader, writer): print('begin') await asyncio.sleep(10000) async def main(): server = await asyncio.start_server( handle, '', 8889) async with server: await server.serve_forever() asyncio.run(main())
# Client import asyncio async def connect(): reader, writer = await asyncio.open_connection( '192.168.81.196', 8889) writer.write(('a'*4*10**9).encode()) # 4*10**9 is large enough that it doesn't get wrong and I can see the effect. await writer.drain() writer.close() await writer.wait_closed() print('connection is closed') async def main(): await connect() asyncio.run(main())