如何使用Asyncio UDP客户端添加超时

发布于 2025-01-29 16:03:27 字数 1559 浏览 4 评论 0原文

我的代码是这样,它基本上只是复制python中的udp echo client的示例代码
官方文档。

import asyncio
from asyncio import DatagramProtocol


class EchoClientProtocol(DatagramProtocol):
    def __init__(self, message, on_con_lost):
        self.message = message
        self.on_con_lost = on_con_lost
        self.transport = None

    def connection_made(self, transport):
        self.transport = transport
        print('Send:', self.message)
        self.transport.sendto(self.message.encode())

    def datagram_received(self, data, addr):
        print("Received:", data.decode())

        print("Close the socket")
        self.transport.close()

    def error_received(self, exc):
        print('Error received:', exc)

    def connection_lost(self, exc):
        print("Connection closed")
        self.on_con_lost.set_result(True)


async def main():

    loop = asyncio.get_running_loop()

    on_con_lost = loop.create_future()
    message = "Hello World!"
    transport, protocol = await loop.create_datagram_endpoint(
        lambda: EchoClientProtocol(message, on_con_lost),
        remote_addr=('127.0.0.1', 20001)
    )

    try:
        await on_con_lost
    finally:
        transport.close()


asyncio.run(main())

它运行良好。但是当不可用的服务时,客户将被阻止。我尝试添加asyncio.wait_for修复它,但它不起作用。

transport, protocol = await asyncio.wait_for(loop.create_datagram_endpoint(
    lambda: EchoClientProtocol(message, on_con_lost),
    remote_addr=('127.0.0.1', 20001)
), 5)

我猜想也许是因为我并不完全理解 on_con_lost 参数的用法,该怎么做?

my code is like this, it basically just copy the example code of UDP Echo Client in Python
offical documentation.

import asyncio
from asyncio import DatagramProtocol


class EchoClientProtocol(DatagramProtocol):
    def __init__(self, message, on_con_lost):
        self.message = message
        self.on_con_lost = on_con_lost
        self.transport = None

    def connection_made(self, transport):
        self.transport = transport
        print('Send:', self.message)
        self.transport.sendto(self.message.encode())

    def datagram_received(self, data, addr):
        print("Received:", data.decode())

        print("Close the socket")
        self.transport.close()

    def error_received(self, exc):
        print('Error received:', exc)

    def connection_lost(self, exc):
        print("Connection closed")
        self.on_con_lost.set_result(True)


async def main():

    loop = asyncio.get_running_loop()

    on_con_lost = loop.create_future()
    message = "Hello World!"
    transport, protocol = await loop.create_datagram_endpoint(
        lambda: EchoClientProtocol(message, on_con_lost),
        remote_addr=('127.0.0.1', 20001)
    )

    try:
        await on_con_lost
    finally:
        transport.close()


asyncio.run(main())

It works well.But when service is not available, client will be blocked. I tried add asyncio.wait_for to fix it, but it's not working.

transport, protocol = await asyncio.wait_for(loop.create_datagram_endpoint(
    lambda: EchoClientProtocol(message, on_con_lost),
    remote_addr=('127.0.0.1', 20001)
), 5)

I have a guess that maybe because I am not totally understanrd this usage of the on_con_lost parameter, How to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

护你周全 2025-02-05 16:03:27

这是我认为正在发生的事情:

您正在启动数据报端点。
然后,您将在断开未来的启动器块上块。
对于触发的断开未来,必须调用Connection_lost函数。
但是,如果首先没有连接的连接,即连接_made not the connection_made,那么未来就不会准备好。
您真正想要的是添加超时以等待未来。
如果您获得超时过期异常,请关闭连接。
当前代码的工作方式,您启动端点,一旦将代码移开以等待断开的未来,现在无法准备就绪。

Here's what I think is happening:

You are launching your datagram endpoint.
You then have the launcher block on the disconnect future.
For the disconnect future to trigger, the connection_lost function must be called.
But if there was no connection in the first place, where connection_made isnt called, then that future will not become ready.
What you really want is to add the timeout to awaiting the future.
If you get a timeout expired exception then shutdown the connection.
The way your current code works, you launch the endpoint and once that times out the code moves on to wait for the disconnect future which now cannot become ready.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文