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

相关内容

热门资讯

开挂辅助神器"xpo... 大家好,今天小编来为大家解答xpoker辅助神器这个问题咨询软件客服可以免费测试直接加微信(1367...
我来教教大家“wepoker辅... >>您好:wepoker辅助软件安全吗确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
开挂辅助插件"hhp... 开挂辅助插件"hhpoker透视脚本下载"开挂(透视)辅助神器(有挂透视);无需打开直接搜索加(薇:...
推荐攻略“wepoker是不是... >>您好:wepoker是不是技术越好牌就越好确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...
开挂辅助软件"hhp... 【亲,hhpoker辅助器视频 这款游戏可以开挂的,确实是有挂的,很多玩家在这款hhpoker辅助器...
科普分享“wepoker开挂辅... 您好,wepoker开挂辅助方法这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...
开挂辅助神器"德普之... 开挂辅助神器"德普之星怎么设置埋牌"开挂(透视)辅助下载(有挂技术)>>您好:软件加13670430...
我来教大家“wepoker透视... >>>您好:,软件加微信【添加136704302】确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
开挂辅助脚本"德扑圈... 开挂辅助脚本"德扑圈透视"开挂(透视)辅助插件(竟然有挂);打开点击测试直接进入微信(1367043...
一分钟揭秘“WePoKer辅助... 无需打开直接搜索加微信客服(136704302)咨询了解WePoKer辅助透视软件挂辅助器目前(微信...