diff options
author | Lars Wirzenius <liw@liw.fi> | 2011-05-25 17:18:54 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2011-05-25 17:18:54 +0100 |
commit | 675238bc117a77a3f30785f085846b70c593bcee (patch) | |
tree | 624be85cdeab05e526cd872ed46080f5dacb2374 | |
parent | 818761a95e7701d9ca6c27c8f288b54312043dcc (diff) | |
download | bgproc-675238bc117a77a3f30785f085846b70c593bcee.tar.gz |
Implement processing, simplistic, synchronous.
This is just to get the API working enough to get a
feeling if it works well.
-rw-r--r-- | bgproc.py | 13 | ||||
-rw-r--r-- | bgproc_tests.py | 5 |
2 files changed, 15 insertions, 3 deletions
@@ -19,11 +19,15 @@ class BackgroundProcessing(object): '''Manage background processing queues.''' def __init__(self, func): + self.func = func self.pending_requests = 0 + self.requests = [] + self.results = [] def enqueue_request(self, request): '''Put a request into queue, to be processed by workers whenever.''' self.pending_requests += 1 + self.requests.append(request) def close_requests(self): '''Signal workers that they can retire. @@ -43,9 +47,12 @@ class BackgroundProcessing(object): because all requests have been processed. ''' - return False + for request in self.requests: + self.results.append(self.func(request)) + return len(self.results) > 0 def __iter__(self): '''Iterate over immediately available results.''' - if False: - yield + for result in self.results: + yield result + diff --git a/bgproc_tests.py b/bgproc_tests.py index 9709636..885a223 100644 --- a/bgproc_tests.py +++ b/bgproc_tests.py @@ -45,3 +45,8 @@ class BackgroundProcessingTests(unittest.TestCase): self.bg.enqueue_request(0) self.assertEqual(self.bg.pending_requests, 1) + def test_processes_stuff(self): + self.bg.enqueue_request(0) + self.assertEqual(self.bg.wait_for_results(), True) + self.assertEqual(list(self.bg), [1]) + |