在芹菜3.1中,做django定期任务 [英] In celery 3.1, making django periodic task

查看:169
本文介绍了在芹菜3.1中,做django定期任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中,事情变化太大了,所以我不能使用3.1。我需要一些帮助。



我阅读了关于在django中进行任务,并阅读定期任务文档。但我不知道如何在django中定期执行任务。我认为这是因为我的低级英语..



在旧版本的Celery中,我导入了 djcelery & crontab ,并在 settings.py 中设置CELERYBEAT_SCHEDULE,并由 manage.py 排除。



但似乎我不能用这种方式执行芹菜de子了。比我应该把CELERYBEAT_SCHEDULE?在doc中的django示例中,他们在proj / proj / celery中设置 os.environ.setdefault('DJANGO_SETTINGS_MODULE','proj.settings')
的.py 。 (1)所以在 settings.py 中设置celerybeat(像以前一样)可以吗?



(2)如果可以的话,在django定期任务将非常有帮助。当我阅读教程时,最令人困惑的部分是文件路径。如果你不想提供一个完整的例子,我将非常感谢你解释我应该做什么工作,设置殴打和执行deamon。



感谢阅读。

解决方案

我假设你已经阅读了 django部分,但是你看到这个示例项目



它不使用调度程序,但如果将其添加到设置.py

  from __future__ import absolute_import 

from celery.schedules导入crontab


CELERYBEAT_SCHEDULE = {
#crontab(hour = 0,minute = 0,day_of_week ='saturday')
'schedule-name':{#示例:'file-backup'
'task':'some_django_app.tasks ....',#example:'files.tasks.cleanup'
'schedule':crontab(...)
},
}

#如果要将调度文件放在相对于您的项目或某项内容:
CELERYBEAT_SCHEDULE_FILENAME =some / path / and / filename

现在对于命令,忘了 manage.py ,直接键入 celery



-B 可以一如既往地打败芹菜。



-A 的芹菜应用程序请注意示例项目的 celery.py 中的这一行: app = Celery('proj')

 芹菜-A proj工人-B -l info 

'django-celery'不需要 如果您需要从管理员管理
的计划,或者如果要通过django的ORM将任务结果存储在DB
中,则仅安装:







CELERYBEAT_SCHEDULER ='djcelery.schedulers.DatabaseScheduler'


Things changed too much in Django, so I can't use 3.1. I need some help.

I read about make a task in django, and read Periodic Tasks document. But I don't know how make periodic tasks in django. I think this becuase of my low level English..

In the older version of Celery, I imported djcelery&crontab and set CELERYBEAT_SCHEDULE in settings.py, and excuted by manage.py.

But it seems that I cannot execute celery deamon by that way anymore. Than where I should put CELERYBEAT_SCHEDULE? In django example in docs, they set os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings') in proj/proj/celery.py. (1) So setting celerybeat in settings.py(like before) is ok?

(2) If you can, a tiny example of making periodic task in django will very helpful. When I was reading tutorials, the most confusing part was File Path. If you don't want to provide a whole example, I will really appreciate if you explain about where I should make tasks, set beat, and executed deamon.

Thanks for reading.

解决方案

I assume you've already read the django section from the docs, but have you seen this example project?

It doesn't use the scheduler but if you add this to settings.py:

from __future__ import absolute_import

from celery.schedules import crontab


CELERYBEAT_SCHEDULE = {
    # crontab(hour=0, minute=0, day_of_week='saturday')
    'schedule-name': {  # example: 'file-backup' 
        'task': 'some_django_app.tasks....',  # example: 'files.tasks.cleanup' 
        'schedule': crontab(...)
    },
}

# if you want to place the schedule file relative to your project or something:
CELERYBEAT_SCHEDULE_FILENAME = "some/path/and/filename"

Now for the commands, forget about manage.py, just type celery directly:

-B enables celery beat as always.

-A specifies the name of the celery app. Note this line in the celery.py of the example project: app = Celery('proj')

celery -A proj worker -B -l info

'django-celery' is not required, install it ONLY if you need to manage the schedule from the admin, or if you want to store task results in the DB through django's ORM:

INSTALLED_APPS += ('djcelery',)

# store schedule in the DB:
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'

这篇关于在芹菜3.1中,做django定期任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆