forked from harshitbansal373/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.py
More file actions
executable file
·37 lines (33 loc) · 882 Bytes
/
priorityqueue.py
File metadata and controls
executable file
·37 lines (33 loc) · 882 Bytes
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
import functools
import queue
import threading
class job:
def __init__(self,priority,description):
self.priority=priority
self.description=description
print('New Job:',description)
return
def __eq__(self,other):
try:
return self.priority==other.priority
except AttributeError:
return NotImplemented
def __lt__(self,other):
try:
return self.priority<other.priority
except AttributeError:
return NotImplemented
q=queue.PriorityQueue()
q.put(job(3,'mid-level job'))
q.put(job(10,'low-level job'))
q.put(job(1,'important-level job'))
def process_job(q):
while True:
next_job=q.get()
print('pocessing job:',next_job.description)
q.task_done()
workers=[threading.Thread(target=process_job,args=(q,)), threading.Thread(target=process_job,args=(q,))]
for w in workers:
w.setDaemon(True)
w.start()
q.join()