Celery+Redis实现异步任务(3)

2020-01-03  本文已影响0人  木叶苍蓝

相关:

Celery-详解
Celery+Redis实现异步任务(1)
Celery+Redis实现异步任务(2)
Celery+Redis实现异步任务(3)

定时任务

创建文件夹apps
创建/apps/celery_conf.py

from kombu import Queue

BROKER_URL = 'redis://localhost:6379/1'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/2'

CELERY_IMPORTS = (
      'apps.beat_task'
)
# 队列
CELERY_QUEUES = (
      Queue('baet_task', routing_key='beat_task')
)
# 设置定时任务
CELERYBEAT_SCHEDULE = {
      'my_beat':{
            'task':'apps.beat_task.beat_test'
            # 'schedule':timedelta(seconds=5), # 每隔5秒钟
            # 'schedule':crontab(minute='49', hour='11') # 定点执行
            'schedule':crontab(minute='*/1'),
            'args':(1, 2),
            'kwargs':{"name":"xxxx"},
            'options':{
                    'queue':'beat_test'
                    'routing_key':'beat_task'
             }
      }
}

创建 /apps/__init__.py

from celery import Celery

app = Celery('test_task')

app.config_from_object('apps.celery_conf')

创建 /apps/beat_task.py

import time
from apps import app

@app.task()
def beat_test(x, y, name):
      print name
      return "hello celery beat"

启动命令:

celery -B -A  apps worker -l INFO -Q baet_task
定时任务参数说明:

注意:
默认值都是*,表示任意时刻
举例

上一篇 下一篇

猜你喜欢

热点阅读