<tfoot id='f9wg3'></tfoot>

      <i id='f9wg3'><tr id='f9wg3'><dt id='f9wg3'><q id='f9wg3'><span id='f9wg3'><b id='f9wg3'><form id='f9wg3'><ins id='f9wg3'></ins><ul id='f9wg3'></ul><sub id='f9wg3'></sub></form><legend id='f9wg3'></legend><bdo id='f9wg3'><pre id='f9wg3'><center id='f9wg3'></center></pre></bdo></b><th id='f9wg3'></th></span></q></dt></tr></i><div id='f9wg3'><tfoot id='f9wg3'></tfoot><dl id='f9wg3'><fieldset id='f9wg3'></fieldset></dl></div>
    1. <small id='f9wg3'></small><noframes id='f9wg3'>

        <bdo id='f9wg3'></bdo><ul id='f9wg3'></ul>

    2. <legend id='f9wg3'><style id='f9wg3'><dir id='f9wg3'><q id='f9wg3'></q></dir></style></legend>

      Python:Queue.Empty 异常处理

      时间:2023-09-29
      • <legend id='yP1hy'><style id='yP1hy'><dir id='yP1hy'><q id='yP1hy'></q></dir></style></legend>

        <i id='yP1hy'><tr id='yP1hy'><dt id='yP1hy'><q id='yP1hy'><span id='yP1hy'><b id='yP1hy'><form id='yP1hy'><ins id='yP1hy'></ins><ul id='yP1hy'></ul><sub id='yP1hy'></sub></form><legend id='yP1hy'></legend><bdo id='yP1hy'><pre id='yP1hy'><center id='yP1hy'></center></pre></bdo></b><th id='yP1hy'></th></span></q></dt></tr></i><div id='yP1hy'><tfoot id='yP1hy'></tfoot><dl id='yP1hy'><fieldset id='yP1hy'></fieldset></dl></div>
        <tfoot id='yP1hy'></tfoot>
      • <small id='yP1hy'></small><noframes id='yP1hy'>

          <bdo id='yP1hy'></bdo><ul id='yP1hy'></ul>
                <tbody id='yP1hy'></tbody>

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

                问题描述

                在与某人就 Python 中的异常处理(由处理队列对象引发)进行了简短的辩论之后,我想我应该把它扔在那里...

                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...

                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()
                

                方法二:

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

                一个论点是方法 1 是错误的,因为队列为空不是错误,因此不应使用 Queue.Empty 异常处理.此外,如果您考虑到任务处理部分可能很大,则使用这种编码方式可能会使调试变得更加困难.

                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.

                另一个论点是,任何一种方式在 Python 中都是可以接受的,如果任务处理量很大,那么在 try/except 之外处理任务可以帮助调试,尽管同意这可能比使用方法 2 看起来更丑.

                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.

                意见?

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

                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

                更新 2:我们俩都不知道队列对象是线程安全的.看起来 try/except 是要走的路!

                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!

                推荐答案

                方法 2 是错误的,因为您分两步进行操作,而可以一步完成.在方法 2 中,您检查队列是否为空,然后稍后(很快,但更晚),尝试获取该项目.如果您有两个线程从队列中拉取项目怎么办?get() 仍然可能因队列为空而失败.如果在您检查项目为空后将其添加到队列中怎么办?这些是错误潜入并发代码的微小机会之窗.

                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()
                

                不要纠结于异常应该是错误".例外只是另一种沟通渠道,使用它们.此处使用else"子句来缩小异常子句的范围.

                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 异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:python threading 具有线程安全的队列生产者-消费者 下一篇:multiprocessing.Manager 嵌套共享对象不适用于队列

                相关文章

                  <bdo id='ni9l7'></bdo><ul id='ni9l7'></ul>

                    <i id='ni9l7'><tr id='ni9l7'><dt id='ni9l7'><q id='ni9l7'><span id='ni9l7'><b id='ni9l7'><form id='ni9l7'><ins id='ni9l7'></ins><ul id='ni9l7'></ul><sub id='ni9l7'></sub></form><legend id='ni9l7'></legend><bdo id='ni9l7'><pre id='ni9l7'><center id='ni9l7'></center></pre></bdo></b><th id='ni9l7'></th></span></q></dt></tr></i><div id='ni9l7'><tfoot id='ni9l7'></tfoot><dl id='ni9l7'><fieldset id='ni9l7'></fieldset></dl></div>
                    <legend id='ni9l7'><style id='ni9l7'><dir id='ni9l7'><q id='ni9l7'></q></dir></style></legend>

                  1. <tfoot id='ni9l7'></tfoot>
                  2. <small id='ni9l7'></small><noframes id='ni9l7'>