【发布时间】:2018-10-02 17:39:18
【问题描述】:
我正在阅读 asyncio 文档中的以下代码。
import asyncio
async def tcp_echo_client(message):
reader, writer = await asyncio.open_connection(
'127.0.0.1', 8888)
print(f'Send: {message!r}')
writer.write(message.encode())
data = await reader.read(100)
print(f'Received: {data.decode()!r}')
print('Close the connection')
writer.close()
await writer.wait_closed()
asyncio.run(tcp_echo_client('Hello World!'))
但是我现在能够理解为什么 reader.read 是可等待的但 writer.write 不是?既然它们都是 I/O 操作,写方法也应该是可等待的吧?
【问题讨论】:
标签: python-3.x sockets tcp async-await python-asyncio