企业如何通过vue小程序开发满足高效运营与合规性需求
1654
2022-11-19
python apscheduler 添加定时任务和修改定时任务时间
1. 设置定时任务和修改时间
# 测试函数def run_test(): print(123)# 获取schedulerdef get_scheduler(): url = "mysql+pymysql://root:123456@21.104.111.130:3306/monotors?charset=utf8" jobstores = { "default": SQLALchemyJobStore(url=url, engine_option={'pool_pre_ping': True, "pool_recycle": 3600*8}) } executors = { 'default': ThreadPoolExecutor(20) } job_defaults = { 'coalesce': False, ',ax_instances': 1 } scheduler = BackgroundScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults) return scheduler@utp_router.get("/daily-utp", description="设置每天00:30启动,也可以收到传入参数, hour 和 minute修改设置")def daily_utp(hour: str = "0", minute: str = "30"): scheduler = get_scheduler() scheduler.start() job_id = "incm_moniotr_upt_v1.0" running_job = scheduler.get_job(job_id) param = {"trigger": "cron", "hour": hour, "minute": minute} if running_job is None: # 该任务不存在 scheduler.add_job(id=job_id, func=run_test, **param) else: # 任务存在,则修改时间 scheduler.reschedule_jon(job_id=job_id, **param) return {"status": "200", "message": "success"}
2. modify_job方式(源码分析)
job_id = "abcd"param = {"trigger": "cron", "hour": 10, "minute": 30}scheduler.reschedule_jon(job_id=job_id, **param)
现在需要使用modify_job方式修改时间,将10:30改为11:50
job_id = "abcd"temp_dict = {"hour": 11, "minute": 50} temp_trigger = scheduler._create_trigger(trigger='cron',trigger_args=temp_dict) result = scheduler.modify_job(job_id=job_id, trigger=temp_trigger)
划重点: 其实上面的那个rescheduler_job也是最终使用modify_job方法,看源码
trigger = self._create_trigger(trigger, trigger_args) now = datetime.now(self.timezone) next_run_time = trigger.get_next_fire_time(None, now) return self.modify_job(job_id, jobstore, trigger=trigger, next_run_time=next_run_time)
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~