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真能买到挂吗,一直存在有挂(有挂辅助)1、起透...
终于懂了!德扑圈有透视吗,微扑... 终于懂了!德扑圈有透视吗,微扑克数据分析,果然是有挂(有挂教学)1、下载好正确养号方法之后点击打开,...
玩家必备教程!hhpoker德... 玩家必备教程!hhpoker德州作弊,pokermaster脚本,真是存在有挂(证实有挂)运辅助工具...
总算明白!hhpoker德州透... 总算明白!hhpoker德州透视,pokermaster辅助器,总是是有挂(有挂透明挂)一、游戏安装...
一起来探讨!来玩德州破解器,w... 一起来探讨!来玩德州破解器,wepoker作弊视频,都是有挂(有挂秘笈)所有人都在同一条线上,像星星...
实测教程!sohoopoker... 实测教程!sohoopoker辅助,pokemmo脚本,本来是有挂(新版有挂)1、模拟器是什么优化,...
2024教程!pokermas... 2024教程!pokermaster脚本,htx矩阵wepoker辅助,竟然是有挂(讲解有挂)wep...
实测交流!wepoker怎么设... 实测交流!wepoker怎么设置透视,wepoker私人局有透视吗,竟然有挂(新版有挂);1、打开软...
必备科技!wepoker破解是... 必备科技!wepoker破解是真的还是假的,hhpoker真的假的,确实存在有挂(有挂实锤)1、玩家...
重大通报!哈糖大菠萝开挂,ag... 重大通报!哈糖大菠萝开挂,agpoker辅助,好像是真的有挂(有挂详细)运辅助工具,进入游戏界面。进...