from apscheduler.schedulers.background import BackgroundScheduler
sched = BackgroundScheduler()
def get_net_price_by_name():
pass
添加任务
sched.add_job(get_net_price_by_name, 'cron', minute=setting_minute, hour=setting_hour, args=[pk],
id=setting_id)
添加任务之后服务器重启会定时任务会失效,所以需要通过id查询定时任务,如果没有则添加
def inital_update_price_setting():
pus_list = Price.objects.filter(status='正常', net_price__status='正常')
jobs = [x.id for x in sched.get_jobs()]
for pus in pus_list:
if not str(pus.id) in jobs:
sched.add_job(get_net_price_by_name, 'cron', minute=pus.minute_time, hour=pus.hour_time,
args=[pus.net_price.id], id=str(pus.id))
print([x.id for x in sched.get_jobs()])
然后定时运行这个任务
@sched.scheduled_job('interval', minutes=30)
def update_price_setting():
from backend.pro_name_standard.standard_init import inital_update_price_setting
inital_update_price_setting()