Bra*_*mon 8 python python-3.x async-await python-asyncio
我对如何使用asyncio.Queue特定的生产者-消费者模式感到困惑,在该模式中,生产者和消费者都可以同时独立运行。
首先,请考虑以下示例,该示例紧随docs中的asyncio.Queue示例:
import asyncio
import random
import time
async def worker(name, queue):
while True:
sleep_for = await queue.get()
await asyncio.sleep(sleep_for)
queue.task_done()
print(f'{name} has slept for {sleep_for:0.2f} seconds')
async def main(n):
queue = asyncio.Queue()
total_sleep_time = 0
for _ in range(20):
sleep_for = random.uniform(0.05, 1.0)
total_sleep_time += sleep_for
queue.put_nowait(sleep_for)
tasks = []
for i in range(n):
task = asyncio.create_task(worker(f'worker-{i}', queue))
tasks.append(task)
started_at = time.monotonic()
await queue.join()
total_slept_for = time.monotonic() - started_at
for task in tasks:
task.cancel()
# Wait until all worker tasks are cancelled.
await asyncio.gather(*tasks, return_exceptions=True)
print('====')
print(f'3 workers slept in parallel for {total_slept_for:.2f} seconds')
print(f'total expected sleep time: {total_sleep_time:.2f} seconds')
if __name__ == '__main__':
import sys
n = 3 if len(sys.argv) == 1 else sys.argv[1]
asyncio.run(main())
Run Code Online (Sandbox Code Playgroud)
关于此脚本,有一个更详细的细节:queue.put_nowait(sleep_for)通过常规的for循环将项目同步放入队列。
我的目标是创建一个使用async def worker()(或consumer())和的脚本async def producer()。两者都应安排为同时运行。没有一个消费者协程明确地与生产者绑定或链接。
我如何修改上面的程序,以便生产者是可以与消费者/工人同时调度的协程?
PYMOTW还有另一个例子。它要求生产者提前知道消费者的数量,None并向消费者发出生产已经完成的信号。
use*_*342 13
我如何修改上面的程序,以便生产者是可以与消费者/工人同时调度的协程?
可以对示例进行概括,而无需更改其基本逻辑:
await,例如使用await producer()或await gather(*producers),等等。await queue.join()这是实现上述内容的示例:
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())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5720 次 |
| 最近记录: |