asyncio.run()函数是否会使用已存在的event_loop?(生产者消费者的asyncio.Queue)
创始人
2024-09-21 05:30:30
0

asyncio.run() 函数将会创建一个新的 event_loop,而非使用已存在的 event_loop。当然,您可以通过 async with 来手动传递一个 event_loop 来使用现有的 event_loop。

下面是使用 asyncio.Queue 实现生产者消费者模式的示例代码,其中 async with 用于手动传递 event_loop:

import asyncio

async def producer(queue: asyncio.Queue): for i in range(5): await asyncio.sleep(1) await queue.put(i) print(f"Producer puts {i} into queue")

async def consumer(queue: asyncio.Queue): while True: item = await queue.get() await asyncio.sleep(2) print(f"Consumer gets {item} from queue")

async def main(): my_queue = asyncio.Queue() async with asyncio.get_running_loop(): producer_task = asyncio.create_task(producer(my_queue)) consumer_task = asyncio.create_task(consumer(my_queue)) await asyncio.gather(producer_task, consumer_task)

asyncio.run(main())

相关内容

热门资讯

技术分享!wepoker辅助插... 技术分享!wepoker辅助插件功能,hhpoker到底可以作弊码,存在挂教程(有挂神器);最新版2...
一分钟揭秘!hhpkoer辅助... 一分钟揭秘!hhpkoer辅助挂是真的吗,wpk模拟器,详细教程(有挂透明);支持多人共享记分板与复...
指导大家!poker辅助器免费... 指导大家!poker辅助器免费安装,wepoker钻石怎么看底牌,切实教程(有挂攻略)1、点击下载安...
一分钟了解!hhpoker辅助... 一分钟了解!hhpoker辅助器,hh poker透视器下载,实用技巧(有挂攻略);《WPK辅助透视...
玩家亲测!aapoker免费透... 1、玩家亲测!aapoker免费透视脚本,wepoker有没有挂,曝光教程(有挂技巧)。2、aapo...
专业讨论!hhpoker脚本,... 专业讨论!hhpoker脚本,聚星ai辅助工具下载,科技教程(有挂技巧);小薇(透视辅助)致您一封信...
一分钟了解!德州局hhpoke... 此外,数据分析德州(德州局hhpoker)辅助神器app还具备辅助透视行为开挂功能,通过对客户德州局...
程序员教你!wepoker辅助... 程序员教你!wepoker辅助器下载,wepoker俱乐部辅助,可靠技巧(有挂软件);大神普及一款德...
玩家攻略推荐!impoker辅... 玩家攻略推荐!impoker辅助,uupoker有透视吗,AI教程(有挂透明);原来确实真的有挂(需...
最新通报!wepoker养号规... 最新通报!wepoker养号规律,we poker辅助器v3.3,总结教程(有挂技巧)1、很好的工具...