-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing_12_10.py
42 lines (30 loc) · 916 Bytes
/
listing_12_10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
LIFO-очередь
"""
import asyncio
from asyncio import LifoQueue, Queue
from dataclasses import dataclass, field
@dataclass(order=True)
class WorkItem:
priority: int
order: int
data: str = field(compare=False)
async def worker(queue: Queue):
while not queue.empty():
work_item: WorkItem = await queue.get()
print(f"Processing element {work_item}")
queue.task_done()
async def main():
lifo_queue = LifoQueue()
work_items = [
WorkItem(3, 1, "Lowest priority 1"),
WorkItem(3, 2, "Lowest priority 2"),
WorkItem(3, 3, "Lowest priority 3"),
WorkItem(2, 4, "Medium priority"),
WorkItem(1, 5, "High priority"),
]
worker_task = asyncio.create_task(worker(lifo_queue))
for work in work_items:
lifo_queue.put_nowait(work)
await asyncio.gather(lifo_queue.join(), worker_task)
asyncio.run(main())