Python:Queue.Empty异常处理 [英] Python: Queue.Empty Exception Handling

查看:7706
本文介绍了Python:Queue.Empty异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过短暂的辩论与某人关于异常处理在Python - 由处理队列对象引发 - 我以为我会把它丢在那里...



方法1:



  import Queue 

q = Queue.Queue()

try:
task = q.get(False)
#Opt 1:处理这里的任务并调用q.task_done()
除了Queue.Empty:
#Handle empty在这里排队
pass

#Opt2:处理这里的任务并调用q.task_done()



方法2:



  import Queue 

q = Queue.Queue()

如果q.empty():
#Handle这里的空队列
else:
task = q.get()
#Handle task here
q.task_done()

一个参数是方法1错误,因为队列为空不是错误,因此不应该使用Queue.Empty异常来处理。此外,如果您认为任务处理部分可能会很大,那么可以使编码更加困难。



另一个参数是在Python中可以接受并且处理try / except之外的任务可以帮助调试,如果任务处理很大,尽管同意这可能比使用方法2更丑陋。



意见? / p>

更新:答案1后来的更多信息通过....
辩论是在方法1在一些多线程代码中使用之后开始的。在这种情况下,代码将获取锁(从threading.Lock对象),并释放它一次返回的任务或Queue.Empty抛出



更新2 :我们都不知道队列对象是线程安全的。

解决方案

方法2是错误的,因为您正在两个步骤中进行操作它可以在一个完成。在方法2中,您检查队列是否为空,然后再检查(很快,但是稍后),尝试获取该项目。如果你有两个线程从队列中拉出物品怎么办? get()仍然可以使用空队列失败。如果一个项目在您检查它是空的之后添加到队列怎么办?这些是一些微小的机会窗口,其中的bug可以并入代码。



一步一步,这是更好的选择。

  import Queue 

q = Queue.Queue()

try:
task = q.get(False)
除了Queue.Empty:
#在这里处理空队列
pass
else:
#处理这里的任务并调用q.task_done )

不要挂断异常应该是错误。例外只是另一个沟通渠道,使用它们。使用else子句来缩小异常子句的范围。


After a short debate with someone about exception handling in Python - sparked by the handling of a queue object - I thought I'd throw it out there...

METHOD 1:

import Queue

q = Queue.Queue()

try:
    task=q.get(False)
    #Opt 1: Handle task here and call q.task_done()
except Queue.Empty:
    #Handle empty queue here
    pass

#Opt2: Handle task here and call q.task_done()

METHOD 2:

import Queue

q = Queue.Queue()

if q.empty():
    #Handle empty queue here
else:
    task = q.get()
    #Handle task here
    q.task_done()

One argument is that Method 1 is wrong because the queue being empty is not an error, and therefore should not be handled using Queue.Empty exception. Additionally, it could make debugging more difficult when coded this way if you consider that the task handling part could potentially large.

The other argument is that either way is acceptable in Python and that handling the task outside of the try/except could aid debugging if task handling is large, although agreed that this might look uglier than using Method 2.

Opinions?

UPDATE: A little more info after answer 1 came through.... The debate was started after method 1 was using in some multithreaded code. In which case, the code will acquire the lock (from a threading.Lock object) and release it either once the task it returned or Queue.Empty is thrown

UPDATE 2: It was unknown to both of us that the the queue object was thread safe. Looks like try/except is the way to go!

解决方案

Method 2 is wrong because you are doing an operation in two steps when it could be done in one. In method 2, you check if the queue is empty, and then later (very soon, but still later), try to get the item. What if you have two threads pulling items from the queue? The get() could still fail with an empty queue. What if an item is added to the queue after you checked that it was empty? These are the sort of tiny windows of opportunity where bugs creep in to concurrent code.

Do it in one step, it's by far the better choice.

import Queue

q = Queue.Queue()

try:
    task = q.get(False)
except Queue.Empty:
    # Handle empty queue here
    pass
else:
    # Handle task here and call q.task_done()

Don't get hung up on "exceptions should be errors". Exceptions are simply another channel of communication, use them. Use the "else" clause here to narrow the scope of the exception clause.

这篇关于Python:Queue.Empty异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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