烧瓶,芹菜,pytest和apply_async倒计时混乱

发布于 2025-02-04 03:46:04 字数 1663 浏览 4 评论 0 原文

我敢肯定这是在某个地方注明的,但是我在旋转我的车轮,所以希望这里有人可以提供帮助。

我正在使用一个使用芹菜的烧瓶应用程序,并且正在使用Pytest进行测试。这是两个测试:

def test_celery_tasks_countdown_apply_async(celery_app, celery_worker):
    r1 = add.apply_async((4, 4), countdown=2)
    r2 = multiply.apply_async((4, 4), countdown=2)
    assert r1.get(timeout=10) == 8
    assert r2.get(timeout=10) == 16

def test_celery_tasks_countdown_signatures(celery_app, celery_worker):
    r1 = add.s(4, 4).apply_async(countdown=2)
    r2 = multiply.s(4, 4).apply_async(countdown=2)
    assert r1.get(timeout=10) == 8
    assert r2.get(timeout=10) == 16

实际任务是这样:

@shared_task
def add(x, y):
    return x + y

@shared_task()
def multiply(x, y):
    return x * y

如果我一个一个人运行它们,则通过它们。

pytest tests/test_tasks.py::test_celery_tasks_countdown_apply_async
pytest tests/test_tasks.py::test_celery_tasks_countdown_signatures

但是,如果我一起运行它们(通过调用整个 test_tasks.pys.py 文件),它们都失败了。

pytest tests/test_tasks.py

我还有其他一些工作(例如,对于 delay )。而且,如果我从中删除 Countdown 选项,则它们都会通过,如果一起运行。

为什么使用 Countdown 选项并将这些测试一起运行会导致故障?

目前,我在 conftest.py 看起来像这样的固定装置:

pytest_plugins = ('celery.contrib.pytest', )

@pytest.fixture(scope='session')
def celery_config():
    return {
        'broker_url': 'redis://localhost:8001',
        'result_backend': 'redis://localhost:8001',
        'task_always_eager': False,
    }

Update

我将这个问题留下来,因为我相信这是一个需要一些文档的有效问题。虽然我仍然不了解问题,也不知道如何在自动化测试的范围内解决该问题,但我成功地将我的任务在Pytest之外运行。

I'm sure this is noted somewhere, but I'm spinning my wheels, so hopefully someone on here can help.

I'm working with a Flask app that uses Celery and I'm testing with Pytest. Here are the two tests:

def test_celery_tasks_countdown_apply_async(celery_app, celery_worker):
    r1 = add.apply_async((4, 4), countdown=2)
    r2 = multiply.apply_async((4, 4), countdown=2)
    assert r1.get(timeout=10) == 8
    assert r2.get(timeout=10) == 16

def test_celery_tasks_countdown_signatures(celery_app, celery_worker):
    r1 = add.s(4, 4).apply_async(countdown=2)
    r2 = multiply.s(4, 4).apply_async(countdown=2)
    assert r1.get(timeout=10) == 8
    assert r2.get(timeout=10) == 16

The actual tasks are like so:

@shared_task
def add(x, y):
    return x + y

@shared_task()
def multiply(x, y):
    return x * y

These pass if I run them one by one.

pytest tests/test_tasks.py::test_celery_tasks_countdown_apply_async
pytest tests/test_tasks.py::test_celery_tasks_countdown_signatures

But if I run them together (by calling the whole test_tasks.py file), they both fail.

pytest tests/test_tasks.py

I've got some other tests (eg, for delay) that work. And if I remove the countdown option from these, they both pass if run together.

Why does using the countdown option and running these tests together cause failure?

Right now, my fixture in conftest.py look like this:

pytest_plugins = ('celery.contrib.pytest', )

@pytest.fixture(scope='session')
def celery_config():
    return {
        'broker_url': 'redis://localhost:8001',
        'result_backend': 'redis://localhost:8001',
        'task_always_eager': False,
    }

UPDATE

I'm leaving this question up as I believe it's a valid question that needs some documentation. While I still do not understand the issue or know how to resolve it within the confines of my automated tests, I have succeeded in getting my to run tasks locally outside of PyTest.

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

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

发布评论

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

评论(1

ペ泪落弦音 2025-02-11 03:46:05

这不是一个完整的答案,而是更多的解决方法,可以使结果保存到后端并完成测试。但是,实际任务未发送到队列,并且倒计时选项似乎被忽略了,这两者都不是我想要的。

@pytest.fixture()
def celery_config():
    return {
        'broker_url': 'redis://localhost:8001',
        'result_backend': 'redis://localhost:8001',
        'task_always_eager': True,
        'task_ignore_result': False,
        'task_store_eager_result': True,
    }

参考在这里

我不确定我是否想在生产中使用它们,所以这只是使我的测试通过。我仍然很想知道为什么这一切都会发生。

Not a complete answer but more of a workaround to get the results to save to the backend and tests to complete. But the actual tasks aren't sent to the queue and the countdown option seems to be ignored, both of which aren't what I'm looking for.

@pytest.fixture()
def celery_config():
    return {
        'broker_url': 'redis://localhost:8001',
        'result_backend': 'redis://localhost:8001',
        'task_always_eager': True,
        'task_ignore_result': False,
        'task_store_eager_result': True,
    }

Reference here.

I'm not sure I want to use these in production, so this is just making my tests pass. I still would love to know why this is all happening.

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