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

相关内容

热门资讯

透视肯定!aapoker辅助挂... 透视肯定!aapoker辅助挂,wepoker辅助器下载,一分钟揭秘(有挂了解)1、下载好aapok...
透视计算!aapoker辅助工... 透视计算!aapoker辅助工具存在吗,hhpoker可以控制牌吗,细节方法(果然有挂);亲真的是有...
透视讲解!wepoker辅助器... 透视讲解!wepoker辅助器下载,wpk辅助ai,程序员教你(有挂透明)1、首先打开最新版本,在首...
透视模拟器!wepoker透视... 透视模拟器!wepoker透视脚本免费下载,wepoker辅助透视软件,2025版教程(切实是有挂)...
透视透视!wepoker作弊辅... 透视透视!wepoker作弊辅助,wepoker透视脚本下载,如何分辨真伪(有挂规律);所有人都在同...
透视辅助!hhpoker辅助下... 透视辅助!hhpoker辅助下载,hhpoker是真的假的,必备教程(本然是真的有挂);揭秘教程安装...
透视规律!wpk有没有脚本,w... 透视规律!wpk有没有脚本,wepoker底牌透视脚本app,指导大家(有挂了解)透视规律!wpk有...
辅助透视!智星德州菠萝透视插件... 辅助透视!智星德州菠萝透视插件工具,poker红龙辅助,AI教程(从来真的有挂)是一款可以让一直输的...
透视存在!智星德州菠萝插件官网... 透视存在!智星德州菠萝插件官网,wpk透视辅助方法,玩家亲测(有挂黑科技);1、实时智星德州菠萝开挂...
透视脚本!智星德州菠萝安装,约... 透视脚本!智星德州菠萝安装,约局吧如何查看是否有挂,安装教程(其实存在有挂)1、很好的工具软件,可以...