diff options
author | Raymond Hettinger <python@rcn.com> | 2006-03-24 20:43:29 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2006-03-24 20:43:29 (GMT) |
commit | fd3fcf0b35a479c3df4999d9bad2337a5e3af140 (patch) | |
tree | 770ddf8cb99340d082dd16db6c9b75762fc025ee /Lib/test/test_queue.py | |
parent | 98bcb7081513eda72d4623e11ddb8cba66310561 (diff) | |
download | cpython-fd3fcf0b35a479c3df4999d9bad2337a5e3af140.zip cpython-fd3fcf0b35a479c3df4999d9bad2337a5e3af140.tar.gz cpython-fd3fcf0b35a479c3df4999d9bad2337a5e3af140.tar.bz2 |
SF Patch #1455676: Simplify using Queues with daemon consumer threads
Adds join() and task_done() methods to track when all enqueued tasks have
been gotten and fully processed by daemon consumer threads.
Diffstat (limited to 'Lib/test/test_queue.py')
-rw-r--r-- | Lib/test/test_queue.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py index b55dd01..17c1def 100644 --- a/Lib/test/test_queue.py +++ b/Lib/test/test_queue.py @@ -221,7 +221,37 @@ def SimpleQueueTest(q): _doBlockingTest(q.get, (), q.put, ('empty',)) _doBlockingTest(q.get, (True, 10), q.put, ('empty',)) +cum = 0 +cumlock = threading.Lock() + +def worker(q): + global cum + while True: + x = q.get() + cumlock.acquire() + try: + cum += x + finally: + cumlock.release() + q.task_done() + +def QueueJoinTest(q): + global cum + cum = 0 + for i in (0,1): + t = threading.Thread(target=worker, args=(q,)) + t.setDaemon(True) + t.start() + for i in xrange(100): + q.put(i) + q.join() + verify(cum==sum(range(100)), "q.join() did not block until all tasks were done") + def test(): + q = Queue.Queue() + QueueJoinTest(q) + QueueJoinTest(q) + q = Queue.Queue(QUEUE_SIZE) # Do it a couple of times on the same queue SimpleQueueTest(q) |