【问题标题】:Tasks delay not working when called from cmd从 cmd 调用时任务延迟不起作用
【发布时间】:2019-06-01 05:13:16
【问题描述】:

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

发生这种情况的任何想法。

对于芹菜工人,我正在使用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

【问题讨论】:

    标签: django python-3.x celery celery-task


    【解决方案1】:

    我探索了 celery 文档,似乎我错过了在我的项目中添加 celery 应用程序__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']
    

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 2021-01-30
      • 2017-12-28
      • 2018-07-14
      • 2011-05-31
      相关资源
      最近更新 更多