简体   繁体   中英

How to put elements on bottom of Python queue

I've a queue that is populated by different threads. I've a thread where I get items from the queue and I send them to another destination. When the last operation fails I want to put again the elements in the queue, but in the bottom of the queue because I want that the dequeuer Thread dequeue them as first priority. Is there a way to do that? Maybe using other class instead of Queue.queue?

How about a deque ?

Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

Here's an implementation -- it's basically Queue.put method with self._put(item) replaced by self.queue.appendleft(item)

from Queue import Queue

class MyQueue(Queue):
    def putleft(self, item, block=True, timeout=None):
        self.not_full.acquire()
        try:
            if self.maxsize > 0:
                if not block:
                    if self._qsize() == self.maxsize:
                        raise Full
                elif timeout is None:
                    while self._qsize() == self.maxsize:
                        self.not_full.wait()
                elif timeout < 0:
                    raise ValueError("'timeout' must be a non-negative number")
                else:
                    endtime = _time() + timeout
                    while self._qsize() == self.maxsize:
                        remaining = endtime - _time()
                        if remaining <= 0.0:
                            raise Full
                        self.not_full.wait(remaining)
            self.queue.appendleft(item)
            self.unfinished_tasks += 1
            self.not_empty.notify()
        finally:
            self.not_full.release()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM