-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.py
50 lines (41 loc) · 1.43 KB
/
consumer.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
47
48
49
50
import sys
import threading
class Consumer(threading.Thread):
"""A thread to read from a queue handling
each item taken with the consumer method or function
"""
def __init__(self, source, consumer=None, cap=None, daemon=True):
super(Consumer, self).__init__()
self.source = source
self.daemon = daemon
self.cap = cap or threading.BoundedSemaphore()
if consumer:
self.consumer = consumer
def __enter__(self):
# This ordering avoids race conditions by allowing
# the option of slightly more than the requested number
# of threads to run for a short period of time
item = self.source.get()
self.cap.acquire()
return item
def __exit__(self, type, value, traceback):
if type != None:
print >> sys.stderr, type, value
self.source.task_done()
self.cap.release()
return True # supress any exception
def run(self):
while True:
with self as item:
self.consumer(item)
def consumer(self, item):
pass
class Filter(Consumer):
def __init__(self, source, sink, filter=None, cap=None, daemon=True):
super(Filter, self).__init__(source, filter, cap, daemon)
self.sink = sink
def run(self):
while True:
with self as item:
result = self.consumer(item)
self.sink.put(result)