【问题标题】:How to configure CELERYBEAT_SCHEDULE in Django settings?如何在 Django 设置中配置 CELERYBEAT_SCHEDULE?
【发布时间】:2016-10-17 08:07:41
【问题描述】:

我可以让它作为一个独立的应用程序运行,但我无法让它在 Django 中运行。

这里是独立代码:

from celery import Celery
from celery.schedules import crontab


app = Celery('tasks')
app.conf.update(
    CELERY_TASK_SERIALIZER='json',
    CELERY_RESULT_SERIALIZER='json',
    CELERY_ACCEPT_CONTENT=['json'],
    CELERY_TIMEZONE='US/Central',
    CELERY_ENABLE_UTC=True,
    CELERYBEAT_SCHEDULE = {
    'test': {
        'task': 'tasks.test',
        'schedule': crontab(),
        },
    }
)

@app.task
def test():
    with open('test.txt', 'a') as f:
        f.write('Hello, World!\n')`

它为 Rabbitmq 服务器提供数据并每分钟写入文件。它就像一个魅力,但是当我尝试让它在 Django 中工作时,我得到了这个错误:

您还记得导入包含此任务的模块吗?或者,也许您正在使用相对进口?请参阅____ 更多信息。

消息体的完整内容是:{'retries': 0, 'eta': None, 'kwargs':{},'taskset':无,'timelimit':[无,无],'回调': 无,'task':'proj.test','args':[],'expires':无,'id': '501ca998-b5eb-4ba4-98a8-afabda9e88dd','utc':真,'errbacks':无, 'chord': None} (246b) Traceback(最近一次通话最后):文件 "/home/user/CeleryDjango/venv/lib/python3.5/site-packages/celery/worker/consumer.py", 第 456 行,在 on_task_received 策略[名称](消息,正文,KeyError:'proj.test' [2016-06-16 01:16:00,051:INFO/Beat] 调度程序:发送到期任务测试(proj.test) [2016-06-16 01:16:00,055: ERROR/MainProcess] 收到未注册 “proj.test”类型的任务。

这是我在 Django 中的代码:

# CELERY STUFF
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'US/Central'
CELERYBEAT_SCHEDULE = {
    'test': {
        'task': 'proj.test',
        'schedule': crontab(),
    }
}

芹菜.py

from __future__ import absolute_import

import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
from django.conf import settings  # noqa

app = Celery('proj')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


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

task.py

from __future__ import absolute_import
from celery import shared_task


@shared_task
def test():
    with open('test.txt', 'w') as f:
        print('Hello, World', file=f)

init.py

from __future__ import absolute_import

from .celery import app as celery_app 

对此的任何想法都非常感谢。谢谢。

【问题讨论】:

    标签: python django celery periodic-task


    【解决方案1】:

    您为什么不尝试以下方法,让我知道它是否适合您。它确实对我有用。

    在settings.py中

    CELERYBEAT_SCHEDULE = {
        'my_scheduled_job': {
            'task': 'run_scheduled_jobs', # the same goes in the task name
            'schedule': crontab(),
        },
    }
    

    在tasks.py中..

    from celery.task import task # notice the import of task and not shared task. 
    
    @task(name='run_scheduled_jobs') # task name found! celery will do its job
    def run_scheduled_jobs():
        # do whatever stuff you do
        return True
    

    但是如果你正在寻找 shared_task 那么..

    @shared_task(name='my_shared_task') # name helps celery identify the functions it has to run
    def my_shared_task():
        # do what you want here..
        return True
    

    我将共享任务用于异步作业..所以我需要从如下函数调用它..

    在views.py/或anywhere.py在你的项目应用中

    def some_function():
        my_shared_task.apply_async(countdown= in_seconds)
        return True
    

    以防万一您忘记了,请记住将您尝试运行的应用程序包含在任务中..

    INSTALLED_APPS = [...
     'my_app'...
    ] # include app
    

    我确信这种方法可以正常工作。谢谢

    【讨论】:

    • 非常感谢。这似乎有效,并证实了我最初的怀疑,即该错误与@shared_tasks 和 Celery 无法看到该任务有关。再次感谢;它奏效了。
    • 这值得一票吗? :P 也将此标记为答案,以便将来对其他人有所帮助:)
    • 不幸的是,现在from celery.task import task 已被弃用,shared_task 用于图书馆。
    • 下午好,我已经尝试处理类似的任务很多天了。你能帮我解决这个问题吗?我将非常感激stackoverflow.com/questions/62801096/…
    猜你喜欢
    • 2012-06-30
    • 2020-11-24
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 2012-06-07
    • 2020-09-14
    • 1970-01-01
    相关资源
    最近更新 更多