queue.empty 并在空时放置 [英] queue.empty and doing a put while empty

查看:54
本文介绍了queue.empty 并在空时放置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含两个元素的队列.我循环使用 get 弹出项目的队列.恐怕一旦我弹出第二个元素,循环就会停止循环,并且由于某些错误,我需要重新处理它.所以我把它放回队列,但它不会因为那时队列是空的.

Let's say I have a queue of two elements. I loop though the queue popping off the items using get. I'm afraid the loop will stop looping once I pop off the second element, and i need to reprocess it because of some error. So I put it back in the queue, but it won't since by then the queue is empty.

我的循环:


while not queue.empty():
  try:
    item = queue.get()
    do stuff(item):
  except Exception as e:
    queue.put(item)
  queue.task_done()

我阅读了有关 Queue.empty 的文档,但对阻塞及其含义感到困惑;Queue.empty()

I read the documentation on Queue.empty but getting confused about blocking and what it means;Queue.empty()

推荐答案

如果你在我看来使用多个线程(你为什么要担心阻塞?),这通常不是一个好主意这样做:

If you are working with multiple threads as it looks like to me (why else would you be concerned about blocking?), it is in general not a good idea to do this:

if not queue.empty():  # or 'while' instead of 'if'
    item = queue.get()

因为另一个线程可能在第一行和第二行之间清空了您的 queue,所以您的 queue.get() 现在阻塞,直到有新的东西进来,这似乎如果你写了那不是预期的行为.基本上 if not queue.empty() 在并发类型的场景中毫无意义.

because another thread could have emptied your queue between the first and the second line, so your queue.get() now blocks until anything new comes in, which seems not to be expected behavior if you wrote that. Basically the if not queue.empty() is meaningless in a concurrency kind of scenario.

正确的做法是:

try:
    while True:
        item = queue.get(block=False)
        done = False
        while not done:
            try:
                do stuff(item)
                done = True
            except Exception as e:
                pass  # just try again to do stuff
        queue.task_done()
except Empty:
    pass  # no more items

除此之外(如果我们假设没有其他线程可以使用您的队列),代码可以正常工作.不过,我对您是否理解(或理解",因为问题很老)阻塞的概念有一些严重的疑问.如果一个函数根据文档被阻塞",那只是意味着你的代码的执行将在那个点暂停,直到某个事件发生.time.sleep(1) 可能是阻塞 1 秒的阻塞函数的最好例子.

Other than that (if we assume, no other thread can consume your queue), the code would work fine. I have some serious doubts though about whether you understand (or 'understood', since the question is old) the concept of blocking. If a function is 'blocking' according to the documentation, that just means that the execution of your code will be halted at that point until some event occurs. time.sleep(1) is maybe the best example of a blocking function that blocks for 1 second.

PS:我知道这已经有些年头了,但仍然......

PS: I know this is some years old, but still...

这篇关于queue.empty 并在空时放置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆