celery 定时任务

时间:2024-07-15 14:34:50

用celery定时任务,定时删除文件夹

tasks.py

from celery import Celery
import os
import shutil app = Celery('demo')
app.config_from_object('celeryconfig') app.conf.beat_schedule = {
'send-every-10-seconds': {
'task': 'tasks.my_task',
'schedule': 100.0,
},
} @app.task
def my_task():
"""
删除文件目录下面的所有文件以及文件中的内容
:return:
""" delList = []
delDir = "./prod"
delList = os.listdir(delDir) for f in delList:
filePath = os.path.join(delDir, f)
if os.path.isfile(filePath):
os.remove(filePath)
print(filePath + " was removed!")
elif os.path.isdir(filePath):
shutil.rmtree(filePath, True)
print("Directory: " + filePath + " was removed!")
定时任务开启方式
celery -A tasks.py worker --loglevel=info --beat