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

相关内容

热门资讯

透视能赢"约局吧游戏... 透视能赢"约局吧游戏挂"详细辅助扑克教程(透视脚本透视)是一款可以让一直输的玩家,快速成为一个“必胜...
透视好友"红龙pok... 透视好友"红龙poker作弊指令"详细辅助曝光教程(免费脚本咨询神器);玩家必备必赢加哟《13670...
透视挂"hhpoke... 透视挂"hhpoker免费透视脚本"详细辅助新2025教程(破解器激活码透视);实战中需综合运用上述...
透视透视"hhpok... 透视透视"hhpoker辅助软件是真的么"详细辅助曝光教程(手机插件软件)1、很好的工具软件,可以解...
透视教学"wepok... 透视教学"wepoker有透视吗"详细辅助规律教程(有辅助插件神器);建议优先通过wepoker有透...
透视智能ai"wep... 透视智能ai"wepoker挂"详细辅助微扑克教程(买脚本靠谱技巧),支持语音通讯、好友开房及战队组...
透视透视"hhpok... 透视透视"hhpoker怎么开透视"详细辅助技巧教程(透视脚本免费下载pc技巧);玩家必备必赢加哟《...
透视计算"wepok... 透视计算"wepoker代打辅助"详细辅助玩家教程(智能辅助插件辅助);致您一封信;亲爱wepoke...
透视系统"拱趴大菠萝... 透视系统"拱趴大菠萝有什么挂"详细辅助2025教程(私人局俱乐部技巧)是一款可以让一直输的玩家,快速...
透视总结"xpoke... 透视总结"xpoker辅助助手"详细辅助线上教程(私人局开挂视频技巧)是一款可以让一直输的玩家,快速...