celery 执行异步任务,延迟任务,定时任务

发布时间 2023-06-22 18:29:07作者: 哈哈哈哼

celery 执行异步任务,延迟任务,定时任务

1 异步任务
	任务.delay(参数)
2 延迟任务
	任务.app_async(args=[].eta=时间对象)#如果没有修改时区,需要使用utc时间
3 定时任务
	需要启动beat和worker
    -beat   定时提交任务进程---》配置在app.comf.beat_schedule的任务
    -worker 执行任务
定时任务
	-使用步骤
    1 在celery.py中
    app.conf.timezone="Asia/shanghai"
    #是否使用utc
    app.conf.enable_ctu=False
    #celery的配置文件
    #任务的定时配置
    app.conf.beat_schedule = {
            'send_sms': {
                'task': 'celery_task.user_task.send_sms',
                # 'schedule': timedelta(seconds=3),  # 时间对象
                # 'schedule': crontab(hour=8, day_of_week=1),  # 每周一早八点
                'schedule': crontab(hour=9, minute=43),  # 每天9点43
                'args': ('18888888', '6666'),
            },
        }
    2 启动beat
    celery -A celery_task beat -l info
    3 启动worker
    celery -A celery_task worker -l info -p eventlet
# 注意点:
	1 启动命令的执行位置,如果是包结构,一定在包这一层
    2 include=['celery_task.order_task'],路径从包名下开始导入,因为我们在包这层执行的命令

如果在公司中,只是做定时任务,还有一个更简单的框架
APSchedule:https://blog.csdn.net/qq_41341757/article/details/118759836