1from queue import PriorityQueue
2
3class PqElement(object):
4 def __init__(self, value: int):
5 self.val = value
6
7 #Custom Compare Function (less than or equsal)
8 def __lt__(self, other):
9 """self < obj."""
10 return self.val > other.val
11
12 #Print each element function
13 def __repr__(self):
14 return f'PQE:{self.val}'
15
16#Usage-
17pq = PriorityQueue()
18pq.put(PqElement(v)) #Add Item - O(Log(n))
19topValue = pq.get() #Pop top item - O(1)
20topValue = pq.queue[0].val #Get top value - O(1)
1import threading, queue
2
3q = queue.Queue()
4
5def worker():
6 while True:
7 item = q.get()
8 print(f'Working on {item}')
9 print(f'Finished {item}')
10 q.task_done()
11
12# turn-on the worker thread
13threading.Thread(target=worker, daemon=True).start()
14
15# send thirty task requests to the worker
16for item in range(30):
17 q.put(item)
18print('All task requests sent\n', end='')
19
20# block until all tasks are done
21q.join()
22print('All work completed')
23