结构:
先放代码:
第一步:
配置celery连接redis:
celery = Celery(app1.name, backend='redis://localhost:6379/3', broker='redis://localhost:6379/1') # 配置好celery的backend和broker
第二步:
配置celery定时任务
celery.conf.CELERYBEAT_SCHEDULE = { "test": { "task": "test", "schedule": timedelta(seconds=10) } }
第三步:
在方法中调用celery的配置来使用celery:
只有使用@某某task他才是一个celery配置
@celery.task(bind=True) def renting_date(self):
而这其中也可以使用任务重试
raise self.retry(exc=e, countdown=3, max_retries=5)
第四步:
调用celery定时任务调用celery任务:
使用 celery方法名.delay() 或者 apply_async()
注意:
在同一个页面调用celery定时任务中的name=' ' 要和celery定时任务中的task=' '相同
如果不是同一个页面的话,那么需要将他写为 你需要使用celery定时任务的方法名的全部路径 而celery定时任务配置中的task=' ' 也是一样