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辅助器免费,wepoker插件辅助,好像有挂(确实有挂)所有人都在同一条线上...
一分钟揭秘!德州透视是真的吗,... 一分钟揭秘!德州透视是真的吗,来玩app 德州 辅助,切实有挂(有挂透视)1、许多玩家不知道辅助怎么...
一分钟揭秘!wpk私人局有透视... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...
必备教程!wepoker破解工... 必备教程!wepoker破解工具,hhpoker德州机器人,切实真的有挂(证实有挂)1)辅助插件:进...
一分钟了解!佛手在线是不是有挂... 一分钟了解!佛手在线是不是有挂,we-poker辅助软件教程,都是有挂(今日头条)1、不需要AI权限...
玩家必看科普!wepoker能... 玩家必看科普!wepoker能不能透视,聚星ai辅助工具激活码,一贯真的有挂(有挂分享)1、上手简单...
我来教教你!aapoker怎么... 我来教教你!aapoker怎么开辅助器,wpk私人辅助,都是存在有挂(有挂方略)一、游戏安装教程牌型...
玩家必看攻略!agpoker辅... 玩家必看攻略!agpoker辅助,we poker辅助器v3.3,原来是有挂(有挂存在)1、模拟器是...
一分钟了解!哈糖大菠萝可以开挂... 一分钟了解!哈糖大菠萝可以开挂吗,wepoker游戏的安装教程,总是存在有挂(了解有挂)1、点击下载...
总算了解!pokemmo脚本辅... 总算了解!pokemmo脚本辅助器下载,wepoker开脚本视频,一直真的有挂(真是有挂)1、超多福...