python - Queue.asyncio值错误: task_done() called too many times - Coding error or a bug detected?

标签 python pytest python-asyncio python-3.7 pytest-asyncio

我实现了一段代码,它从一个队列中获取一个元素,并将相同的对象从队列列表中放入每个队列中。问题是,当我运行特定测试时,我收到 ValueError: task_done() Called too much times 异常。该错误发生在测试代码中,而不是在被测试的代码中。

我正在使用asyncio.Queue并使用协程进行编程。 我将每个 Queue.get 与一个精确的 Queue.task_done 调用相匹配。 我正在使用 pytest 测试代码。

我正在使用以下库:

  • Python 3.7
  • pytest==3.10.0
  • pytest-asyncio==0.9.0

我有两个文件:包含我的类实现的 middleware.py 和实现 pytest 测试的 test_middleware.py

文件middlware.py:

import asyncio

class DistributorMiddleware:

    def __init__(self, in_queue, out_list_queue):
        self._in = in_queue
        self._out = out_list_queue

    async def distribute(self):

        while True:
            ele = await self._in.get()
            count=0
            for queue in self._out:
                await queue.put(ele)
                count+=1
                print(f'inserted ele in {count}')
            queue.task_done()
            if ele == None:
                break
        for queue in self._out:
            await queue.join()

文件test_middleware.py:

import pytest
import asyncio                
from asyncio import Queue
from middleware import DistributorMiddleware
import random
import os


@pytest.mark.asyncio                                                                                     
async def test_distribution(request, event_loop):                                                        
    q_list = [ Queue() for _ in range(10) ]                                                              
    _in = Queue()
    distrib = DistributorMiddleware(_in, q_list)                                                         
    event_loop.create_task(distrib.distribute())                                                         
    num_ele = random.randint(1, 10)
    ele_set = set()
    for _ in range(num_ele):                                                                             
        ele = os.urandom(4)                                                                              
        ele_set.add(ele)
        await _in.put(ele)
    await _in.put(None)                                                                                  
    await asyncio.sleep(1)                                                                               

    for i,q in enumerate(q_list):
        assert q.qsize() == num_ele + 1
        c_set = ele_set.copy()
        count= 0
        while True:
            e = await q.get()
            count+=1
            print(f'Queue {i}: element: "{e}" number {count} extracted of {q.qsize()}!')
            q.task_done()
            if e == None:
                break
            assert e in c_set
            c_set.remove(e)

在测试中,中间件应该从输入队列中获取元素并将它们放入列表中的 10 个队列中。并且它可以正确地完成工作。

测试代码从 10 个队列中的每一个中获取所有元素,并检查它们是否存在于原始队列中。对于第 9 个队列,一切顺利,没有错误,但是当测试尝试从第十个列表中获取第一个元素时,会引发 ValueError :

request = <FixtureRequest for <Function 'test_distribution'>>, event_loop = <_UnixSelectorEventLoop running=False closed=False debug=False>

    @pytest.mark.asyncio
    async def test_distribution(request, event_loop):
        q_list = [ Queue() for _ in range(10) ]
        _in = Queue()
        distrib = DistributorMiddleware(_in, q_list)
        event_loop.create_task(distrib.distribute())
        num_ele = random.randint(1, 10)
        ele_set = set()
        for _ in range(num_ele):
            ele = os.urandom(4)
            ele_set.add(ele)
            await _in.put(ele)
        await _in.put(None)
        await asyncio.sleep(1)

        for i,q in enumerate(q_list):
            assert q.qsize() == num_ele + 1
            c_set = ele_set.copy()
            count= 0
            while True:
                e = await q.get()
                count+=1
                print(f'Queue {i}: element: "{e}" number {count} extracted of {q.qsize()}!')
>               q.task_done()

test_middlewares.py:34: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <Queue at 0x7f7af5b9d828 maxsize=0 _queue=[b'\x15\xad\t\xaf', b'\x8b\xa2M=', None]>

    def task_done(self):
        """Indicate that a formerly enqueued task is complete.

        Used by queue consumers. For each get() used to fetch a task,
        a subsequent call to task_done() tells the queue that the processing
        on the task is complete.

        If a join() is currently blocking, it will resume when all items have
        been processed (meaning that a task_done() call was received for every
        item that had been put() into the queue).

        Raises ValueError if called more times than there were items placed in
        the queue.
        """
        if self._unfinished_tasks <= 0:
>           raise ValueError('task_done() called too many times')
E           ValueError: task_done() called too many times

/usr/lib/python3.7/asyncio/queues.py:202: ValueError

每个get都匹配一个task_done。我可以验证对 test_middlware.py 文件进行以下修改:

-            q.task_done()
+            try:
+                q.task_done()
+            except ValueError as err:
+                print(f'Value Error: {err}')
+                print(q.qsize())

这样做我可以看到,即使引发了许多 ValueError ,元素仍然从队列中检索。测试成功:

platform linux -- Python 3.7.1, pytest-3.10.0, py-1.7.0, pluggy-0.8.0
rootdir: /tmp/stack, inifile:
plugins: asyncio-0.9.0
collected 1 item                                                                                                                                                                                                  

test_middlewares.py .                                                                                                                                                                                       [100%]

============================================================================================ 1 passed in 1.04 seconds =============================================================================================

为了确保测试消耗所有列表中的所有元素,我在测试结束时添加了错误断言,强制出现错误:

             assert e in c_set
             c_set.remove(e)

+    assert False == True
+

结果输出显示从所有列表中检索了所有元素,但最后一个队列上的每个 task_done 都会生成 ValueError

Queue 7: element: "b'\x9b\xf8m\x02'" number 1 extracted of 3!
Queue 7: element: "b'\x15\xad\t\xaf'" number 2 extracted of 2!
Queue 7: element: "b'\x8b\xa2M='" number 3 extracted of 1!
Queue 7: element: "None" number 4 extracted of 0!
Queue 8: element: "b'\x9b\xf8m\x02'" number 1 extracted of 3!
Queue 8: element: "b'\x15\xad\t\xaf'" number 2 extracted of 2!
Queue 8: element: "b'\x8b\xa2M='" number 3 extracted of 1!
Queue 8: element: "None" number 4 extracted of 0!
Queue 9: element: "b'\x9b\xf8m\x02'" number 1 extracted of 3!
============================================================================================ 1 failed in 1.06 seconds ==

问题是,我是否遗漏了某些内容并且代码中有错误或者我发现了错误?

最佳答案

您的代码有错误。事实上,queue.task_done() 只应在从队列中取出元素时调用,而不是在将它们放入队列时调用。

但是您的中间件类正在它刚刚使用 .put() 的队列上调用它,这是 self._out 列表中的最后一个队列;从 DistributorMiddleware.distribute() 中删除 queue.task_done() 调用:

async def distribute(self):

    while True:
        ele = await self._in.get()
        count=0
        for queue in self._out:
            await queue.put(ele)
            count+=1
            print(f'inserted ele in {count}')
        queue.task_done()
        # ^^^^^ you didn't take anything from the queue here!

当您删除该行时,您的测试就会通过。

您在测试中看到异常的原因是因为只有那时队列才知道task_done()被调用得太频繁。 DistributorMiddleware.distribute() 中的 queue.task_done() 调用会将未完成的任务计数器减 1,但仅当该计数器降至以下时零可以检测到异常。只有当最后一个任务在 test_distribution() 中从队列中取出时,您才会到达这一点,此时未完成的任务计数器至少提前了一步达到 0。

也许这应该是对 self._in.task_done() 的调用?您刚刚在 while 循环中从该队列中获取了一个元素:

async def distribute(self):

    while True:
        ele = await self._in.get()
        # getting an element from self._in
        count=0
        for queue in self._out:
            await queue.put(ele)
            count+=1
            print(f'inserted ele in {count}')
        self._in.task_done()
        # done with ele, so decrement the self._in unfinished tasks counter

关于python - Queue.asyncio值错误: task_done() called too many times - Coding error or a bug detected?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53218629/

相关文章:

python - Asyncio.gather 与 asyncio.wait

python-3.x - 将方法移动到另一个 Python 文件

python - 如何选择数据行

python - 在 Gunicorn 中使用 aiohttp 和 aiopg 时如何设置日志记录?

python - 无法在装饰器中捕获 pytest 的结果

python - 有没有办法覆盖pytest(python)中的默认断言?

python - 如何针对不同版本的 python 运行 py.test?

python - 为什么 telnetlib write 函数在我的 python 代码中不起作用?

python - 如何将 Pandas 中的两位数年份整数转换为四位数字?

python-3.x - 如何使用 asyncio 优雅地超时