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私人局规律,德普之星透视辅助,本来真的是有挂(有挂方针)该软件可以轻松地帮...
热点推荐!胡易辅助软件,打两圈... 热点推荐!胡易辅助软件,打两圈兴化辅助,详细教程(有挂解密)暗藏猫腻,小编详细说明打两圈兴化辅助破解...
经核实!pokemmo脚本手机... 经核实!pokemmo脚本手机版,德普辅助app,一直是有挂(有挂透视)进入游戏-大厅左侧-新手福利...
盘点十款!白金岛跑胡子脚本,老... 盘点十款!白金岛跑胡子脚本,老友麻将开挂辅助器,详细软件(有挂方略)1、全新机制【老友麻将开挂辅助器...
玩家亲测!微信途游有辅助吗,雀... 您好,微信途游有辅助吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩...
据公告内容!拱趴大菠萝万能挂,... 据公告内容!拱趴大菠萝万能挂,德普之星辅助器,好像是有挂(真实有挂)1、德普之星辅助器公共底牌简单,...
总算了解!洞庭茶苑辅助,微乐山... 总算了解!洞庭茶苑辅助,微乐山西脚本插件,详细软件(竟然有挂)亲,关键说明,微乐山西脚本插件透视脚本...
教程辅助挂!aa poker辅... 教程辅助挂!aa poker辅助,德普之星透视辅助,竟然真的有挂(有挂方式)所有人都在同一条线上,像...
发现一款!微乐多乐跑作弊,创思... 发现一款!微乐多乐跑作弊,创思维激k辅助器免费,详细攻略(有挂存在)创思维激k辅助器免费透视方法中分...
随着!wpk辅助是什么,德扑之... 随着!wpk辅助是什么,德扑之星私人局辅助器,总是确实有挂(有挂分享)所有人都在同一条线上,像星星一...