Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- from asyncio import Queue
- from loguru import logger
- async def processor_1(q1: Queue):
- while True:
- item = await q1.get()
- logger.info(f"processor_1: processing {item}")
- q1.task_done()
- async def processor_2(q2: Queue):
- while True:
- item = await q2.get()
- logger.info(f"processor_2: processing {item}")
- q2.task_done()
- async def producer(q1, q2):
- for i in range(10):
- await q1.put(i)
- await q2.put(i * 2)
- async def main():
- q1 = Queue()
- q2 = Queue()
- tasks = [
- asyncio.create_task(processor_1(q1)),
- asyncio.create_task(processor_2(q2)),
- ]
- await producer(q1, q2)
- await q1.join()
- await q2.join()
- for t in tasks:
- t.cancel()
- if __name__ == '__main__':
- asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement