【问题标题】:How can I maintain two active asyncio streams without a TimeoutError?如何在没有 TimeoutError 的情况下维护两个活动的 asyncio 流?
【发布时间】: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


【解决方案1】:

它挂起是因为工人死锁等待一些外来消息。 一条建议,总是使用超时。

【讨论】:

    猜你喜欢
    • 2012-08-05
    • 2016-06-27
    • 2012-06-24
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多