0

My ImageStitcher class is receiving multiple image messages from different threads. Another thread will then call the get_stichted_image() and this works. But it doesn't look good and seems kinda slow.

Is there a better way to handle multiple incoming messages from different threads and wait for all queues (or something else) to contain something?

class ImageStitcher:
    def __init__(foo):
        self.image_storage = {
            Position.top: queue.Queue(maxsize=1),
            Position.bottom: queue.Queue(maxsize=1)
        }
        foo.register(image_callback)

    # will be called from different threads
    def image_callback(self, image_msg):
        if self.image_storage[image_msg["position"]].full():
            self.image_storage[image_msg["position"].get()
        self.image_storage[image_msg["position"].put(image_msg)

    def get_stichted_image(self):
        try:
            # the following code is ugly and seems to be slow
            top_image_msg = self.image_storage[Position.top].get(timeout=0.1)
            bottom_image_msg = self.image_storage[Position.bottom].get(timeout=0.1)
            return self.stitch_images(top_image_msg, bottom_image_msg)
        except queue.Empty:
            return None

  • 1
    I am wondering if there is a confusing between Threading and Multiprocessing. Assuming that you don't have to perform I/O operations, you should actually use Multiprocessing. Please see here: https://stackoverflow.com/questions/3044580/multiprocessing-vs-threading-python – Sztyler Oct 30 '20 at 23:00
  • I am a bit dubious about the above comment. I think for this case you would need threading instead of multi processing to leverage the shared memories and objects. As for what you are looking for —- your question is rather broad, I would advise you to look into blogs about thread management with queues and so on. – AzyCrw4282 Oct 30 '20 at 23:09
  • This callback will actually called from an I/O thread. But it doesn't actually matter if it's threads or processes, the problem remains the same. – ExceptionFinder Oct 30 '20 at 23:11
  • You could use a "Condition" to "wait" for a new item in one of the queues (a single condition for all queues). If a new item is put, condition is notified and the waiting thread checks all queues for the new item. – Michael Butscher Oct 30 '20 at 23:30

0 Answers0