我正在编写一些代码来使用asyncio包(python 3.5和Unix系统)来传递两个进程。
我为该任务创建了一个unix域套接字,使用以下代码作为服务器进程:
factory = asyncio.start_unix_server(listen, SOCKET_ADDR)
server = event_loop.run_until_complete(factory)
event_loop.run_forever()
其中listen是一个接收数据并打印其长度的协程:
async def listen(reader, writer):
data_stream = await reader.read()
writer.close()
print(len(data_stream))
客户端进程只连接到该套接字并发送二进制字符串:
# Coroutine that sends a binary string
async def send_data(socket_address):
_, writer = await asyncio.open_unix_connection(socket_address)
datastream = b'fooo'*18*1024
print(len(datastream))
writer.write(datastream)
await writer.drain()
writer.close()
# Connect to the socket and send the data
event_loop = asyncio.get_event_loop()
event_loop.run_until_complete(send_data(SOCKET_ADDR))
我不明白为什么客户端生成的数据流大小与服务器中收到的data_stream不同。事实上,我总是得到相同的结果,所以我认为它不是随机的。这些是我获得的不同结果:
datastream = b'fooo'*4*1024 --> Client-length 16384 // Server-length 16384 (Correct)
datastream = b'fooo'*5*1024 --> Client-length 20480 // Server-length 16384 (Uncompleted)
datastream = b'fooo'*18*1024 --> Client-length 73728 // Server-length 16384 (Uncompleted)
datastream = b'fooo'*19*1024 --> Client-length 77824 // Server-length 73728 (Uncompleted)
我错过了什么?谢谢!