celery初级教程(七)

本文介绍如何使用Celery配置并执行周期性任务。通过示例展示了如何设置Celery Beat Schedule来定时运行任务,包括固定间隔和基于Cron表达式的复杂调度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们如何使用celery执行一些周期性任务呢?

celery beat是一个调度器,它可以周期内指定某个worker来执行某个任务。如果我们想周期执行某个任务需要增加beat_schedule配置信息. 

celeryconfig.py配置信息如下:

ROKER_URL = 'redis://:332572@127.0.0.1:6379/1'

CELERY_RESULT_BACKEND = 'redis://:332572@127.0.0.1:6379/2'

CELERYBEAT_SCHEDULE = {
    'every-5-minute':
        {
            'task': 'proj.tasks.period_task',
            'schedule': 5.0,
            'args': (16, 16),
        }
}

CELERY_ROUTES = ({
    'proj.tasks.send_email': {'queue': 'queue1'},
    'proj.tasks.upload_file': {'queue': 'queue1'},
    'proj.tasks.image_process': {'queue': 'queue2'},
    },
    # route_for_task,
)

CELERYBEAT_SCHEDULE配置每隔5秒执行一次period_task任务, 'every-5-minute'为任务名称,可自定义。task指定周期执行的任务函数,schedule指定多久执行一次task任务, args为传递给任务函数的参数。

tasks.py模块内容如下:

from proj.celery import app as celery_app
import time

# 创建任务函数
@celery_app.task
def send_email():
    print("正在发送email....")
    time.sleep(5)
    print("email发送完毕!")

@celery_app.task
def upload_file():
    print("正在处理文件上传任务....")
    time.sleep(3)
    print("文件上传完毕!")

@celery_app.task
def image_process():
    print("正在处理图像....")
    time.sleep(5)
    print("图像处理完毕!")

@celery_app.task
def period_task(a, b):
    print("开始周期性任务...")
    time.sleep(3)
    print("周期性任务执行完毕:%d!" % (a + b))

启动woker处理周期性任务:

celery -A proj worker --loglevel=info --beat

在命令后面加上--beat,表示启动周期性任务。


如果我们想指定在某天某时某分某秒执行某个任务,可以执行cron任务, 增加配置信息如下:

CELERYBEAT_SCHEDULE = {
    'every-5-minute':
        {
            'task': 'proj.tasks.period_task',
            'schedule': 5.0,
            'args': (16, 16),
        },
    'add-every-monday-morning': {
        'task': 'proj.tasks.period_task',
        'schedule': crontab(hour=7, minute=30, day_of_week=1),
        'args': (16, 16),
    },

}

crontab编写的例子:

例子含义
crontab()每分钟执行
crontab(minute=0, hour=0)Execute daily at midnight.
crontab(minute=0, hour='*/3')Execute every three hours: midnight, 3am, 6am, 9am, noon, 3pm, 6pm, 9pm.
crontab(minute=0,
hour='0,3,6,9,12,15,18,21')
Same as previous.
crontab(minute='*/15')Execute every 15 minutes.
crontab(day_of_week='sunday')Execute every minute (!) at Sundays.
crontab(minute='*',
hour='*', day_of_week='sun')
Same as previous.
crontab(minute='*/10',
hour='3,17,22',day_of_week='thu,fri')
Execute every ten minutes, but only between 3-4 am, 5-6 pm, and 10-11 pm on Thursdays or Fridays.
crontab(minute=0, hour='*/2,*/3')Execute every even hour, and every hour divisible by three. This means: at every hour except: 1am, 5am, 7am, 11am, 1pm, 5pm, 7pm, 11pm
crontab(minute=0, hour='*/5')Execute hour divisible by 5. This means that it is triggered at 3pm, not 5pm (since 3pm equals the 24-hour clock value of “15”, which is divisible by 5).
crontab(minute=0, hour='*/3,8-17')Execute every hour divisible by 3, and every hour during office hours (8am-5pm).
crontab(0, 0, day_of_month='2')Execute on the second day of every month.
crontab(0, 0,
day_of_month='2-30/3')
Execute on every even numbered day.
crontab(0, 0,
day_of_month='1-7,15-21')
Execute on the first and third weeks of the month.
crontab(0, 0, day_of_month='11',
month_of_year='5')
Execute on the eleventh of May every year.
crontab(0, 0,
month_of_year='*/3')
Execute on the first month of every quarter.

开启一个celery beat服务:

celery -A proj beat
celery需要保存上次任务运行的时间在数据文件中,文件在当前目录下名字叫celerybeat-schedule. beat需要访问此文件:
celery -A proj beat -s /home/celery/var/run/celerybeat-schedule


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值