Python 更改FIFO队列中的第一个项目,并将其推回第一个位置

Python 更改FIFO队列中的第一个项目,并将其推回第一个位置,python,Python,是否可以更改FIFO队列中的第一个项目并将其推回第一个位置?我似乎找不到任何相关的资源。以下是我到目前为止所取得的成绩,但它将推进到最后一项 import Queue q = Queue.Queue(maxsize=0) # multiple q.put_nowait(msg) msg = q.get_nowait() # edit the msg q.put_nowait(msg) 任何帮助都将不胜感激,谢谢 你在寻找类似的东西;这不是a的标准操作,因为它违反了FIFO(先进先出)。因

是否可以更改FIFO队列中的第一个项目并将其推回第一个位置?我似乎找不到任何相关的资源。以下是我到目前为止所取得的成绩,但它将推进到最后一项

import Queue

q = Queue.Queue(maxsize=0)
# multiple q.put_nowait(msg) 

msg = q.get_nowait()
# edit the msg
q.put_nowait(msg)

任何帮助都将不胜感激,谢谢

你在寻找类似的东西;这不是a的标准操作,因为它违反了FIFO(先进先出)。因此,它也不是Python模块提供的操作。但有一种容器类型非常适合此任务:。这包括append和appendleft操作。因此,您可以将其与put=appendleft、get=pop、unget=append一起使用

  • 你为什么要这么做
  • 这是物体吗
  • 如果是这样,您可以访问底层的
    .queue
    属性,该属性本身就是。它确实支持
    appendleft
    方法

    尽管如此,我强烈建议不要这样做,这违反了FIFO接口,而且这样做是有原因的。如果它不适合你的需要,找一些适合你的<代码>队列使用put/pop进行操作是有原因的,在简单的
    deque

    示例“unget队列”上有很多逻辑。基本上,我是从cpython队列函数复制代码的。应该和put()函数一样安全

    注意:这仅适用于cpython 3。见资料来源:

    导入队列
    类UngetQueue(queue.queue):
    def unget(自身、项目、块=真、超时=无):
    ''将一个项目放入队列的头部。
    如果可选参数“block”为true,“timeout”为None(默认值),
    如有必要,阻塞,直到有空闲插槽可用。如果“超时”为
    非负数,它最多阻止“超时”秒并引发
    如果在此时间内没有可用插槽,则完全例外。
    否则('block'为false),如果有空闲插槽,则将项目放入队列
    立即可用,否则引发完全异常(“超时”
    在这种情况下被忽略)。
    '''
    使用self.not_full:
    如果self.maxsize>0:
    如果不阻止:
    如果self.qsize()>=self.maxsize:
    提高
    elif超时为无:
    当self.qsize()>=self.maxsize时:
    self.not_full.wait()
    elif超时<0:
    raise VALUERROR(“'timeout'必须是非负数”)
    其他:
    endtime=time()+超时
    当self.qsize()>=self.maxsize时:
    剩余=结束时间-时间()
    
    如果剩余,我认为没有办法在不将其从
    队列中移除的情况下检索顶部。所以答案可能是否定的。不,没有办法做到这一点。您可以使用,它允许您
    追加
    追加左
    ,以及
    弹出
    弹出
    谢谢!我正在基于现有代码添加新功能。也许我必须改用deque,首先仔细调查一下它为什么会在那里。对deque的简单调用不是线程安全的,这就是为什么
    Queue
    existsdeque的append和pop操作是线程安全的,但是如果您执行pop-then-append,则另一个线程很可能在两者之间执行了某些操作。这和偷窥手术不一样。
    import queue
    
    class UngetQueue(queue.Queue):
        def unget(self, item, block=True, timeout=None):
            '''Put an item into the head of the queue.
            If optional args 'block' is true and 'timeout' is None (the default),
            block if necessary until a free slot is available. If 'timeout' is
            a non-negative number, it blocks at most 'timeout' seconds and raises
            the Full exception if no free slot was available within that time.
            Otherwise ('block' is false), put an item on the queue if a free slot
            is immediately available, else raise the Full exception ('timeout'
            is ignored in that case).
            '''
            with self.not_full:
                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._unget(item)
                self.unfinished_tasks += 1
                self.not_empty.notify()
    
        def _unget(self, item):
            self.queue.appendleft(item)