Process items from queue with lock.
(consumer_id: str)
| 211 | await asyncio.sleep(0.01) # Simulate arrival rate |
| 212 | |
| 213 | async def consumer(consumer_id: str): |
| 214 | """Process items from queue with lock.""" |
| 215 | while True: |
| 216 | try: |
| 217 | item = await asyncio.wait_for(queue.get(), timeout=0.5) |
| 218 | except asyncio.TimeoutError: |
| 219 | break |
| 220 | |
| 221 | async with lock: |
| 222 | # Simulate processing |
| 223 | await asyncio.sleep(0.02) |
| 224 | processed_order.append((consumer_id, item["id"])) |
| 225 | queue.task_done() |
| 226 | |
| 227 | # Start producer and two consumers concurrently |
| 228 | producer_task = asyncio.create_task(producer(5)) |
no test coverage detected