簡體   English   中英

Django 周期性任務 celery

[英]Django periodic task celery

我是 Django 和 Celery 的新手。 請幫助我,我不明白,它是如何工作的。 我想每 1 分鍾在我的控制台中看到“Hello world”。

任務.py

from celery import Celery
from celery.schedules import crontab
from celery.task import periodic_task

app = Celery('tasks', broker='pyamqp://guest@localhost//')


@periodic_task(run_every=(crontab(hour="*", minute=1)), ignore_result=True)
def hello_world():
    return "Hello World"

celery.py

from __future__ import absolute_import

import os

from celery import Celery

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test.settings.local")

app = Celery('test')
app.config_from_object('celeryconfig')
app.autodiscover_tasks()


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

初始化文件

from __future__ import absolute_import

# 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

芹菜配置文件

broker_url = 'redis://localhost:6379'
result_backend = 'rpc://'

task_serializer = 'json'
result_serializer = 'json'
accept_content = ['json']
timezone = 'Europe/Oslo'
enable_utc = True

這是一個簡單的 celery 設置和代碼,但不起作用 =\

celery -A tasks worker -B

什么也沒有發生。 告訴我我做錯了什么? 謝謝!

您需要在 celeryconfig.py 中設置 beat_schedule

from celery.schedules import crontab
beat_schedule = {
    'send_each_minute': {
        'task': 'your.module.path.function',
        'schedule': crontab(),
        'args': (),
    },
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM