Queue limitations?

Metalone jcb at iteris.com
Thu Mar 16 22:34:06 EST 2006


This example is missing a few initialization details although they can
possibly be inferred.
For example is
iq = imageQueue()
but imageQueue does not have a put() method.
Is SetQueue() called?
Is iq.start() called?

I like to see small, fully complete and tested examples.
The following works using strings as images.
It might prove interesting to modify this test case to use your image
objects instead of strings.

import Queue
import threading
import unittest

class imageQueue(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.filenum = 0
        self.theQueue = Queue.Queue(-1)
        self.done = False
        self.results = []

    def run(self):
        while not self.done:
            # for testing, do something simple with images
            try:
                img = self.theQueue.get(True, 1)
            except:
                pass
            else:
                self.results.append(img)
                self.filenum += 1

    def SetQueue(self, q_):
        self.theQueue = q_

    def stop(self):
        self.done = True


class Tester(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_1(self):
        # initialize
        q = Queue.Queue()
        iq = imageQueue()
        iq.SetQueue(q)
        iq.start()

        # produce images
        images = ["123", "456", "789"]
        for x in images:
            q.put(x)

        # wait till all images consumed
        while iq.filenum != 3:
            pass

        # stop the thread
        iq.stop()

        # assert that the consumer consumed what was produced
        msg = "%s != %s" % (images, iq.results)
        self.assert_(images == iq.results, msg)

if __name__ == '__main__':
    unittest.main()




More information about the Python-list mailing list