从 cmd 调用时任务延迟不起作用



我在一个 cmd 和 shell 中运行芹菜工人,我正在运行我的任务,但是当我像TestTaskOne.delay()一样调用我的任务时,它不起作用,cmd 只是暂停在那里,我必须以 ctrl+c 终止,工人也没有收到任何任务。

任何想法为什么会发生这种情况。

对于我正在使用的芹菜工人celery -A Project worker -l info -P eventlet

tasks.py

from __future__ import absolute_import, unicode_literals
from celery import task
@task
def TestTaskOne():
    msg = "DEFAULT   TASK   IS   WORKING......"
    return msg

celery.py

from __future__ import absolute_import, unicode_literals
import os, logging
from celery import Celery
from celery.schedules import crontab

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'RestUserAPI.settings')
app = Celery('UserAPI')
# Using a string here means the worker don'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()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

settings.py

CELERY_BROKER_URL = 'mongodb://localhost:27017'
CELERY_RESULT_BACKEND = "mongodb"
CELERY_IGNORE_RESULT = False
CELERY_TRACK_STARTED = True
CELERY_MONGODB_SCHEDULER_DB = "celery"
CELERY_MONGODB_SCHEDULER_COLLECTION = "schedules"
CELERY_MONGODB_SCHEDULER_URL = "mongodb://localhost:27017"
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE

我浏览了芹菜文档,似乎错过了在我的项目中添加芹菜应用程序__init__.py

添加这些行后,延迟功能开始工作。

__init__py

from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ['celery_app']

最新更新