Get all items from thread Queue

后端 未结 6 1654
别那么骄傲
别那么骄傲 2021-02-13 05:14

I have one thread that writes results into a Queue.

In another thread (GUI), I periodically (in the IDLE event) check if there are results in the queue, like this:

6条回答
  •  情书的邮戳
    2021-02-13 05:24

    If you're always pulling all available items off the queue, is there any real point in using a queue, rather than just a list with a lock? ie:

    from __future__ import with_statement
    import threading
    
    class ItemStore(object):
        def __init__(self):
            self.lock = threading.Lock()
            self.items = []
    
        def add(self, item):
            with self.lock:
                self.items.append(item)
    
        def getAll(self):
            with self.lock:
                items, self.items = self.items, []
            return items
    

    If you're also pulling them individually, and making use of the blocking behaviour for empty queues, then you should use Queue, but your use case looks much simpler, and might be better served by the above approach.

    [Edit2] I'd missed the fact that you're polling the queue from an idle loop, and from your update, I see that the problem isn't related to contention, so the below approach isn't really relevant to your problem. I've left it in in case anyone finds a blocking variant of this useful:

    For cases where you do want to block until you get at least one result, you can modify the above code to wait for data to become available through being signalled by the producer thread. Eg.

    class ItemStore(object):
        def __init__(self):
            self.cond = threading.Condition()
            self.items = []
    
        def add(self, item):
            with self.cond:
                self.items.append(item)
                self.cond.notify() # Wake 1 thread waiting on cond (if any)
    
        def getAll(self, blocking=False):
            with self.cond:
                # If blocking is true, always return at least 1 item
                while blocking and len(self.items) == 0:
                    self.cond.wait()
                items, self.items = self.items, []
            return items
    

提交回复
热议问题