-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_threads.py
46 lines (40 loc) · 1.28 KB
/
worker_threads.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
43
44
45
46
from Queue import PriorityQueue
from threading import Thread, active_count
from time import sleep, time as now
from random import uniform
t0 = now()
def worker(id):
def workfunc():
while True:
item = q.get()
try:
t = uniform(1, 3)
sleep(t)
if item == 7:
print "[Thread %s] I cannot process item %s." % (id, item)
raise Exception
print "Thread <%2s> processed item <%2s> in %.2f seconds. Active threads: %d." % (id, item, t, active_count())
except:
# I didn't do my job, so I let someone else do it
q.put(item + 0.1)
finally:
q.task_done()
return workfunc
q = PriorityQueue()
num_worker_threads = 5
for i in range(num_worker_threads):
t = Thread(target=worker(i))
t.daemon = True
t.start()
source = range(20)
for item in source:
q.put(item)
try:
while not q.empty():
sleep(0.1)
q.join() # block until all tasks are done
except KeyboardInterrupt:
# Okay, so you want to stop? Will work only before call to join.
raise SystemExit(1)
finally:
print "All done in %.2f seconds." % (now() - t0)