Get all items from thread Queue

后端 未结 6 1749
别那么骄傲
别那么骄傲 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:35

    I'd be very surprised if the get_nowait() call caused the pause by not returning if the list was empty.

    Could it be that you're posting a large number of (maybe big?) items between checks which means the receiving thread has a large amount of data to pull out of the Queue? You could try limiting the number you retrieve in one batch:

    def queue_get_all(q):
        items = []
        maxItemsToRetrieve = 10
        for numOfItemsRetrieved in range(0, maxItemsToRetrieve):
            try:
                if numOfItemsRetrieved == maxItemsToRetrieve:
                    break
                items.append(q.get_nowait())
            except Empty, e:
                break
        return items
    

    This would limit the receiving thread to pulling up to 10 items at a time.

提交回复
热议问题