芹菜不在 django 中工作,只是在等待(待定)

celery not working in django and just waiting (pending)

我正在尝试了解芹菜的工作原理。我有一个项目大约有 10 app.now 我想使用 celery .

setting.py:

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

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

这是我的 test.py:

from .task import when_task_expiration

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

task.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() 时 pending.i 尝试替换 @shared_task@app.task(bind=True) 但注意到 changed.even 我尝试使用 .delay() 而不是 apply_async((2, 2), countdown=3) 并且再次没有发生任何事情。

我正在尝试使用 celery 在 期间的特定时间调用一个函数,我在 past.thank 你问过。

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