summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-05-25 18:35:05 +0100
committerLars Wirzenius <liw@liw.fi>2011-05-25 18:35:05 +0100
commit34f67e179194b4530162369cba3b456ab3463599 (patch)
treea998f0338776d1410e6ed2cfb7d980601d073181
parent742cfae9ddc793015f01d371b70373dcbfd909cd (diff)
downloadbgproc-34f67e179194b4530162369cba3b456ab3463599.tar.gz
Put back a "pending" attribute.
Wasn't so hard, after all.
-rw-r--r--bgproc.py8
-rw-r--r--bgproc_tests.py10
2 files changed, 14 insertions, 4 deletions
diff --git a/bgproc.py b/bgproc.py
index fe15c5d..db148b2 100644
--- a/bgproc.py
+++ b/bgproc.py
@@ -34,7 +34,7 @@ class BackgroundProcessing(object):
'''Manage background processing queues.'''
def __init__(self, func, numprocs=None):
- self._pending_requests = 0
+ self.pending = 0
self.requests = multiprocessing.Queue()
self.results = multiprocessing.Queue()
args = (func, self.requests, self.results)
@@ -46,7 +46,7 @@ class BackgroundProcessing(object):
def enqueue_request(self, request):
'''Put a request into queue, to be processed by workers whenever.'''
- self._pending_requests += 1
+ self.pending += 1
self.requests.put((request,))
def close_requests(self):
@@ -73,7 +73,7 @@ class BackgroundProcessing(object):
'''
- if block and self._pending_requests == 0:
+ if block and self.pending == 0:
return []
items = []
@@ -83,7 +83,7 @@ class BackgroundProcessing(object):
except Queue.Empty:
break
else:
- self._pending_requests -= 1
+ self.pending -= 1
items.append(item)
block = False
return items
diff --git a/bgproc_tests.py b/bgproc_tests.py
index 3f3634d..92e48cd 100644
--- a/bgproc_tests.py
+++ b/bgproc_tests.py
@@ -32,11 +32,21 @@ class BackgroundProcessingTests(unittest.TestCase):
self.bg.close_requests()
self.bg.finish()
+ def test_has_no_pending_initially(self):
+ self.assertEqual(self.bg.pending, 0)
+
def test_get_results_returns_nothing_initially(self):
self.assertEqual(self.bg.get_results(), [])
def test_processes_stuff(self):
self.bg.enqueue_request(0)
+
+ # The following is not a race condition, because the counter
+ # only gets decremented after get_results has retrieved the result,
+ # and we only call that later.
+ self.assertEqual(self.bg.pending, 1)
+
self.assertEqual(self.bg.get_results(block=True), [1])
+ self.assertEqual(self.bg.pending, 0)
self.assertEqual(self.bg.get_results(block=True), [])