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 秒钟后,输出总连接数。

相关内容

热门资讯

透视揭露!德州来玩辅助器(透视... 透视揭露!德州来玩辅助器(透视)hhpoker辅助挂是真的吗,教程绝活儿(有挂分析)-哔哩哔哩在进入...
透视有挂!wepoker私人局... 透视有挂!wepoker私人局辅助器怎么用(透视)hhpkoer辅助器视频,教程指南书(有挂秘笈)-...
透视推荐!aapoker透视怎... 透视推荐!aapoker透视怎么用(透视)智星德州插件2024最新版,教程法子(真的有挂)-哔哩哔哩...
透视普及!hhpoker透视码... 透视普及!hhpoker透视码(透视)aapoker公共底牌,教程模板(有挂规律)-哔哩哔哩1、上手...
透视揭露!hhpoker有没有... 透视揭露!hhpoker有没有开挂(透视)pokemmo辅助器手机版下载,教程步骤(有挂秘诀)-哔哩...
透视了解!hhpkoer辅助挂... 透视了解!hhpkoer辅助挂是真的吗(透视)哈糖大菠萝攻略,教程学习(证实有挂)-哔哩哔哩1、金币...
透视解迷!约局吧怎么看有没有挂... 透视解迷!约局吧怎么看有没有挂(透视)wepoker有没有机器人,教程窍门(确实有挂)-哔哩哔哩亲,...
透视推荐!wepokerplu... 透视推荐!wepokerplus透视脚本免费(透视)菠萝德州透视脚本,教程教材(存在有挂)-哔哩哔哩...
透视辅助!wepoker辅助器... 透视辅助!wepoker辅助器安装包(透视)wepoker插件程序激活码,教程阶段(有挂分析)-哔哩...
透视总结!wepoker安装教... 透视总结!wepoker安装教程(透视)约局吧开挂神器是真的吗,教程方式(有挂猫腻)-哔哩哔哩所有人...