python的schedule定时任务模块二次封装方法
Python  /  管理员 发布于 7年前   163
通过定时来执行任务,我们日常工作生活中会经常用到。python有schedule这个库,简单好用,比如,可以每秒,每分,每小时,每天,每天的某个时间点,间隔天数的某个时间点定时执行,另外自己又写了一个可以自定义时间点来定时执行任务,代码如下。
import scheduleimport time class Timing(): #按秒循环定时执行任务 def doEverySecond(self,seconds,job_func): try: schedule.every(seconds).seconds.do(job_func) while True: schedule.run_pending() except Exception as e: raise e # 按分钟循环定时执行任务 def doEveryMinutes(self,minutes,job_func): try: schedule.every(minutes).minutes.do(job_func) while True: schedule.run_pending() except Exception as e: raise e # 按小时循环定时执行任务 def doEveryHours(self,Hours,job_func): try: schedule.every(Hours).minutes.do(job_func) while True: schedule.run_pending() except Exception as e: raise e #按天数在某个时刻定时执行任务 def doEveryDay(self,time,job_func,days=1): try: schedule.every(days).days.at(time).do(job_func) while True: schedule.run_pending() except Exception as e: raise e #设置在每天的多个时刻定时执行任务,这个方法在实际工作中比较常用到 def doEveryTime(self,time_str,job_func,days=1): ''' :param time_str: :param job_func: :param days: :return: None example:time_str="10:30","10:45","11:00" ''' try: list_time = time_str.split(",") for time in list_time: schedule.every(days).days.at(time).do(job_func) while True: schedule.run_pending() except Exception as e: raise e #自定义时间,dateTimes格式为:"2018-06-08 16:55,2018-06-08 16:56" def doJustTime(self,datestr,job_func): try: date_list = datestr.split(",") for i in date_list: #转换为unix时间戳格式 timeArray = time.strptime(i, "%Y-%m-%d %H:%M") timestamp = time.mktime(timeArray) while True: now_time = round(time.time(),0) if timestamp == now_time: job_func() break else: time.sleep(1) except Exception as e: raise e if __name__ == "__main__": def print1(): print("ok") Timing().doJustTime('2018-06-08 17:53,2018-06-08 17:54',print1)
以上这篇python的schedule定时任务模块二次封装方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号