asyncio的create_datagram_endpoint中connection_made被调用了多少次?
创始人
2024-09-21 06:30:09
0

在 asyncio 的 create_datagram_endpoint 方法中,当建立了连接之后,connection_made 方法会被调用。所以,我们可以通过重写 connection_made 方法来进行统计,记录该方法被调用的次数。

以下是一个示例:

import asyncio

class ConnectionCounterProtocol:
    def __init__(self):
        self.connection_count = 0

    def connection_made(self, transport):
        self.connection_count += 1
        print(f"Connection made. Connection count: {self.connection_count}")

    def connection_lost(self, exc):
        print(f"Connection lost.")
        asyncio.get_event_loop().stop()

async def main():
    _, protocol = await asyncio.create_datagram_endpoint(
        lambda: ConnectionCounterProtocol(),
        local_addr=('localhost', 8888)
    )

    await asyncio.sleep(5)

    print(f"Total connection count: {protocol.connection_count}")

if __name__ == "__main__":
    asyncio.run(main())

在上面的示例中,我们重写了 ConnectionCounterProtocol 类中的 connection_made 方法,用来统计 connection_made 被调用的次数,并在每次连接建立时打印出连接数。

最后,我们在主函数中调用了 create_datagram_endpoint 方法,并传入 ConnectionCounterProtocol 的实例,以建立连接。等待 5 秒钟后,输出总连接数。

相关内容

热门资讯

透视教你!pokemomo辅助... 透视教你!pokemomo辅助软件(透视)hhpkoer辅助器,教程项目(竟然有挂)-哔哩哔哩pok...
wpk德州局怎么透视!wepo... wpk德州局怎么透视!wepoker国外版透视(透视)教程-切实必备真的有挂wpk德州局怎么透视辅助...
透视有挂!hhpoker视频巡... 透视有挂!hhpoker视频巡查真的假的(透视)hhpoker免费辅助器,教程攻略(有挂教学)-哔哩...
hhpoker开挂教程!wep... hhpoker开挂教程!wepoker透视苹果系统(透视)技巧-都是解谜是有挂1)wepoker透视...
透视解迷!hhpoker德州真... 透视解迷!hhpoker德州真的假的(透视)wepoker祈福有用吗,教程讲义(有挂透明挂)-哔哩哔...
德州局透视!菠萝辅助器免费版的... 德州局透视!菠萝辅助器免费版的特点(透视)app-好像了解真的有挂1、下载好菠萝辅助器免费版的特点透...
pokerrrr2辅助!wep... pokerrrr2辅助!wepoker有没有辅助(透视)插件-切实解谜真的有挂wepoker有没有辅...
透视曝光!pokemmo脚本辅... 透视曝光!pokemmo脚本辅助(透视)wepoker怎么设置透视,教程演示(发现有挂)-哔哩哔哩w...
wepoker怎么拿到好牌!德... wepoker怎么拿到好牌!德州之星扫描器(透视)软件-果然专业有挂1)wepoker怎么拿到好牌免...
透视必备!fishpoker透... 您好,wejoker辅助机器人这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...