通过定时来执行任务,我们日常工作生活中会经常用到。python有schedule这个库,简单好用,比如,可以每秒,每分,每小时,每天,每天的某个时间点,间隔天数的某个时间点定时执行,另外自己又写了一个可以自定义时间点来定时执行任务,代码如下。
import schedule import 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定时任务模块二次封装方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 梁汉文2008-EDMONDHITS48[香港]3CD[WAV+CUE]
- 国风大师纯音系列《童丽-[渭城曲]》1CD[WAV分轨][593.5MB]
- 国风大师纯音系列《许菱子《烧筝2》古筝曲集》1CD[WAV分轨][1.3GB]
- 王子异《1996 Part.2》[320K/MP3][18.29MB]
- 群星.1994-宝丽金最倾情3CD【宝丽金】【WAV+CUE】
- 乐俊亨.2024-同泪【CharioticMusic】【FLAC分轨】
- 何耀珊.2006-收获【华纳】【WAV+CUE】
- 梁汉文1994-壹精选[华星][WAV+CUE]
- 洪荣宏1981-歹路不可行·相思雨(飞跃复刻版)[光美][WAV+CUE]
- 旅行团2015-10Day’s[来福胶泥][WAV+CUE]
- 龙千玉.2011-卡将哟【豪记】【WAV+CUE】
- 蔣志光.1993-多情多寂寞(国专)【BMG】【WAV+CUE】
- 陈慧娴.1996-雪映美白96演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 王子异《1996 Part.2》[FLAC/分轨][50.66MB]
- 贵族音乐《古典名曲与轻音乐钢琴 打开宝宝睡眠开关》[320K/MP3][155.91MB]