【发布时间】:2019-05-11 07:56:30
【问题描述】:
我正在尝试使用 asyncio 来管理 p2p 网络应用程序中的连接。我正在尝试使用 asyncio 流来维护大量(约 300 个)连接。
我使用的是 python3.6,它每次都会在 asyncio.open_connection(...) 上挂起并超时。
async def example():
reader, writer = await asyncio.open_connection(ip, port)
writer.write(handshake)
await writer.drain()
response = await reader.read(RESP_SIZE)
errcode, results = await worker(reader, writer, workerdata)
# This is the line it hangs and times out on
reader2, writer2 = await asyncio.open_connection(ip2, port2)
# Second, identical handshake sequence here
writer2.write(handshake)
await writer2.drain()
response = await reader2.read(RESP_SIZE)
errcode, results = await worker(reader2, writer2, workerdata2)
def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(example())
loop.close()
一个简单的示例适用于单个连接,但是一旦我尝试执行握手/打开第二个连接,它就会挂起并收到
TimeoutError: [Errno 110] Connect call failed
是否可以使用异步流同时与不同的客户端 IP/端口对建立多个连接?是否有其他更适合此的异步库?
【问题讨论】:
-
在用睡眠代码替换工作人员时无法重现您的问题。使用 Python 3.7
标签: python asynchronous networking async-await python-3.6