gpt4 book ai didi

django - celery 不在 django 中工作,只是在等待(待定)

转载 作者:行者123 更新时间:2023-12-05 03:00:14 25 4
gpt4 key购买 nike

我正在尝试了解 celery 的工作原理。我有一个大约有 10 个应用程序的项目。现在我想使用 celery 。

设置.py:

CELERY_BROKER_URL = 'amqp://rabbitmq:rabbitmq@localhost:5672/rabbitmq_vhost'
CELERY_RESULT_BACKEND = 'redis://localhost'

我使用以下信息在 rabbitmq 中创建了一个用户:username: rabbitq and password:rabbitmq 。然后我创建一个名为 rabbitmq_vhost 的虚拟主机,并为其添加 rabbitmq 权限。我认为一切都很好,因为关于 rabbitmq 的所有错误都消失了。

这是我的测试.py:

from .task import when_task_expiration

def test_celery():
result = when_task_expiration.apply_async((2, 2), countdown=3)
print(result.get())

任务.py:

from __future__ import absolute_import, unicode_literals
import logging

from celery import shared_task
from proj.celery import app

@app.task
def when_task_expiration(task, x):
print(task.id, 'task done')
return True

celery .py:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

app = Celery('proj')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

现在,当我在 python shell 中调用 test_celery() 时,它处于待处理状态。我尝试替换 @shared_task@app.task(bind=True) 但注意到已更改。即使我尝试使用 .delay() 而不是 apply_async((2, 2), countdown=3) 仍然没有任何反应。

我正在尝试在 this quesation 期间的特定时间使用 celery 调用函数我过去问过。谢谢。

最佳答案

您很可能忘记运行至少一个 Celery 工作进程。为此,请在 shell 中执行以下命令:celery worker -A proj.celery -c 4 -l DEBUG(这里我假设你的 Celery 应用程序是在 proj/celery.py 中定义的,因为你有 Celery('proj') 在里面)

关于django - celery 不在 django 中工作,只是在等待(待定),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56966228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com