diff options
author | rcoh <rcoh@mit.edu> | 2011-08-02 22:02:50 -0700 |
---|---|---|
committer | rcoh <rcoh@mit.edu> | 2011-08-02 22:02:50 -0700 |
commit | 0dcc566d84f109a6026c60aeaebe4187a34f892d (patch) | |
tree | 9b75742349abe421272f730dc62fc17142c9b8a8 | |
parent | ea209dc1b261a9e6358204c6d93f71854394e08c (diff) |
adding driver classes and a demo of how to use them.
-rw-r--r-- | driverdemo.py | 30 | ||||
-rw-r--r-- | drivers.py | 27 |
2 files changed, 57 insertions, 0 deletions
diff --git a/driverdemo.py b/driverdemo.py new file mode 100644 index 0000000..1c359b5 --- /dev/null +++ b/driverdemo.py @@ -0,0 +1,30 @@ +from drivers import * +from threading import * +import Queue +class TestProcessor(Processor): + def do(self): + data = [] + if not self.input_queue.empty(): + data.append(self.input_queue.get(False)) + self.again(.1) + self.done(data) + +class TestListener(Listener): + def handle(self, data): + print data + print active_count() + +def addToQueue(): + queue.put("test") + again = Timer(.5, addToQueue) + again.start() + +queue = Queue.Queue() +t = TestProcessor(queue) +l = TestListener() +t.listeners.append(l) +queue = t.input_queue +t.do() +addToQueue() + + diff --git a/drivers.py b/drivers.py new file mode 100644 index 0000000..0c14018 --- /dev/null +++ b/drivers.py @@ -0,0 +1,27 @@ +import Queue +from threading import Timer + +class Processor(object): + def __init__(self, input_queue): + self.input_queue = input_queue + self.listeners = [] + def again(self, t): + next_run = Timer(t, self.do) + next_run.start() + def do(self): + """ + The do method should: + Consider input in the input queue + call self.again(timeDelay) + call self.done(data) + In that order + """ + raise NotImplementedError("proc must be defined") + def done(self, data): + [l.handle(data) for l in self.listeners] + #todo [rcoh] - there should be a better way to do this. + + +class Listener(object): + def handle(self, data): + raise NotImplementedError("handle must be defined") |