清除队列中的所有项目

发布于 2024-11-17 13:38:44 字数 125 浏览 0 评论 0原文

我怎样才能清除队列。例如,我在队列中有数据,但由于某种原因我不需要现有数据,只想清除队列。

有什么办法吗?这行得通吗:

oldQueue = Queue.Queue()

How can I clear a queue. For example I have datas in a queue, but for some reason I don't need the existing data, and just want to clear the queue.

Is there any way? Will this work:

oldQueue = Queue.Queue()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

娇纵 2024-11-24 13:38:44
q = Queue.Queue()
q.queue.clear()

编辑
为了清楚和简洁起见,我省略了线程安全的问题,但@Dan D 是相当正确的,以下更好。

q = Queue.Queue()
with q.mutex:
    q.queue.clear()
q = Queue.Queue()
q.queue.clear()

EDIT
I omitted the issue of thread safety for clarity and brevity, but @Dan D is quite correct, the following is better.

q = Queue.Queue()
with q.mutex:
    q.queue.clear()
娇妻 2024-11-24 13:38:44

您只是无法清除队列,因为每个 put 还会添加 unfinished_tasks 成员。
连接方法取决于该值。
并且 all_tasks_done 也需要被通知。

with q.mutex:
    q.queue.clear()
    q.all_tasks_done.notify_all()
    q.unfinished_tasks = 0

或者以适当的方式,使用 get 和 task_done 对来安全地清除任务。

    while not q.empty():
        try:
            q.get(block=False)
        except Empty:
            continue
        q.task_done()

或者只是创建一个新队列并删除旧队列。

You just can not clear the queue, because every put also add the unfinished_tasks member.
The join method depends on this value.
And all_tasks_done needs to be notified also.

with q.mutex:
    q.queue.clear()
    q.all_tasks_done.notify_all()
    q.unfinished_tasks = 0

or in decent way, use get and task_done pair to safely clear the tasks.

    while not q.empty():
        try:
            q.get(block=False)
        except Empty:
            continue
        q.task_done()

or just create a new Queue and delete old one.

你的往事 2024-11-24 13:38:44

这对我来说似乎做得很好。我欢迎评论/补充,以防我错过任何重要的内容。

class Queue(queue.Queue):
  '''
  A custom queue subclass that provides a :meth:`clear` method.
  '''

  def clear(self):
    '''
    Clears all items from the queue.
    '''

    with self.mutex:
      unfinished = self.unfinished_tasks - len(self.queue)
      if unfinished <= 0:
        if unfinished < 0:
          raise ValueError('task_done() called too many times')
        self.all_tasks_done.notify_all()
      self.unfinished_tasks = unfinished
      self.queue.clear()
      self.not_full.notify_all()

This seems to do it pretty well for me. I welcome comments/additions in case I missed anything important.

class Queue(queue.Queue):
  '''
  A custom queue subclass that provides a :meth:`clear` method.
  '''

  def clear(self):
    '''
    Clears all items from the queue.
    '''

    with self.mutex:
      unfinished = self.unfinished_tasks - len(self.queue)
      if unfinished <= 0:
        if unfinished < 0:
          raise ValueError('task_done() called too many times')
        self.all_tasks_done.notify_all()
      self.unfinished_tasks = unfinished
      self.queue.clear()
      self.not_full.notify_all()
怂人 2024-11-24 13:38:44

仅使用公共 API:

try:
    while True:
        self.queue.get(False)
except queue.Empty:
    pass

Using only the public API:

try:
    while True:
        self.queue.get(False)
except queue.Empty:
    pass
夏夜暖风 2024-11-24 13:38:44

如果您只想要一个空队列,并且不关心旧队列的垃圾收集,只需使用相同的句柄实例化一个新队列即可。例如,

    q = Queue(maxsize=max_size)
    q.put(1)
    q.put(2)
    q.put(3)
    q.put(4)

现在为了使该队列为空,您可以简单地编写以下内容。

    q = Queue(maxsize=max_size)

此时您应该有一个空队列。

If you just want an empty queue, and don't care about garbage collection of the older queue, simply instantiate a new queue using the same handle. For example,

    q = Queue(maxsize=max_size)
    q.put(1)
    q.put(2)
    q.put(3)
    q.put(4)

Now in order to make this queue empty, you can simply write the following.

    q = Queue(maxsize=max_size)

And at this point you should have an empty queue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文