asyncio 队列被设计成与 queue 模块类似。尽管asyncio队列不是线程安全的,但是他们是被设计专用于async/await 代码。 注意asyncio 的队列没有timeout 形参
import asyncio, random, time
async def rnd_sleep(t):
# sleep for T seconds on average
await asyncio.sleep(t * random.random() * 2)
async def producer(queue):
while True:
token = random.random()
print(f'produced {token}')
if token < .05:
break
await queue.put(token)
await rnd_sleep(.1)
async def consumer(queue):
while True:
token = await queue.get()
await rnd_sleep(.3)
queue.task_done()
print(f'consumed {token}')
async def main():
queue = asyncio.Queue()
# fire up the both producers and consumers
producers = [asyncio.create_task(producer(queue))
for _ in range(3)]
consumers = [asyncio.create_task(consumer(queue))
for _ in range(10)]
# with both producers and consumers running, wait for
# the producers to finish
await asyncio.gather(*producers)
print('---- done producing')
# wait for the remaining tasks to be processed
await queue.join()
# cancel the consumers, which are now idle
for c in consumers:
c.cancel()
asyncio.run(main())
本文详细介绍了如何使用Python的AsyncIO库中的队列进行异步编程,包括创建生产者和消费者任务,以及如何等待这些任务完成。通过示例代码展示了AsyncIO队列的工作原理和其在并发编程中的作用。
2895

被折叠的 条评论
为什么被折叠?



