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())

相关内容

热门资讯

重大通报!pokermaste... 重大通报!pokermaster辅助器,wepoker模拟器哪个好用,解密教程(有挂攻略);支持多人...
透视规律!wpk模拟器多开(透... 透视规律!wpk模拟器多开(透视)一贯是有挂(详细辅助透明挂教程)1、首先打开最新版本,在首页我们可...
透视ai代打!hhpoker辅... 透视ai代打!hhpoker辅助软件下载(透视)总是是有挂(详细辅助解说技巧)1、该软件可以轻松地帮...
记者揭秘!poker辅助器免费... 1、记者揭秘!poker辅助器免费安装,wepoker透视苹果系统,技巧教程(有挂技巧)。2、pok...
透视辅助!约局吧是否有挂(透视... 透视辅助!约局吧是否有挂(透视)真是存在有挂(详细辅助揭秘教程);1、机器人多个强度级别选择2、发牌...
记者揭秘!hhpoker哪个俱... 1、记者揭秘!hhpoker哪个俱乐部靠谱,德州辅助工具到底怎么样,扑克教程(有挂攻略);详细教程。...
免费测试版!wepoker辅助... 1、免费测试版!wepoker辅助分析器,拱趴大菠萝自动计算机器人,分享教程(有挂方法)2、进入游戏...
透视新版!wepoker可以透... 透视新版!wepoker可以透视码(透视)好像有挂(详细辅助攻略教程);1、完成的残局,帮助玩家取得...
科普常识!aapoker发牌逻... 科普常识!aapoker发牌逻辑,拱趴大菠萝万能挂图解,透明挂教程(有挂技巧);人气非常高,ai更新...
透视攻略!wepoker怎么拿... 透视攻略!wepoker怎么拿到好牌(透视)真是有挂(详细辅助揭秘教程)一、AI软件牌型概率发牌机制...