zoukankan      html  css  js  c++  java
  • 自定義的PYTHON定時框架

      1 import schedule
      2 import os
      3 import datetime
      4 import threading
      5 import time
      6 
      7 
      8 class Timing:
      9     def __init__(self):
     10         self.gLock = threading.Lock()
     11 
     12     # 按秒循环定时执行任务
     13     def doBySecond(self, job_func, joblist):
     14         try:
     15             for jobdic in joblist:
     16                 # 判断定时任务的类型,按天、日、分、秒
     17                 if jobdic['schedule_type'] == 'second':
     18                     interval = jobdic['interval']
     19                     kwargs = jobdic['kwargs']
     20                     schedule.every(interval).seconds.do(job_func, **kwargs)
     21         except Exception as e:
     22             raise e
     23 
     24     # 按分钟循环定时执行任务
     25     def doByMinutes(self, job_func, joblist):
     26         try:
     27             for jobdic in joblist:
     28                 # 判断定时任务的类型,按天、日、分、秒
     29                 if jobdic['schedule_type'] == 'minute':
     30                     interval = jobdic['interval']
     31                     time = jobdic['time']
     32                     kwargs = jobdic['kwargs']
     33                     schedule.every(interval).minutes.at(time).do(job_func, **kwargs)
     34         except Exception as e:
     35             raise e
     36 
     37     # 按小时循环定时执行任务
     38     def doByHours(self, job_func, joblist):
     39         try:
     40             for jobdic in joblist:
     41                 # 判断定时任务的类型,按天、日、分、秒
     42                 if jobdic['schedule_type'] == 'hour':
     43                     interval = jobdic['interval']
     44                     time = jobdic['time']
     45                     kwargs = jobdic['kwargs']
     46                     schedule.every(interval).hour.at(time).do(job_func, **kwargs)
     47         except Exception as e:
     48             raise e
     49 
     50     # 按天数在某个时刻定时执行任务
     51     def doByDay(self, job_func, joblist):
     52         try:
     53             for jobdic in joblist:
     54                 # 判断定时任务的类型,按天、日、分、秒
     55                 if jobdic['schedule_type'] == 'day':
     56                     interval = jobdic['interval']
     57                     time = jobdic['time']
     58                     kwargs = jobdic['kwargs']
     59                     print(kwargs)
     60                     # self.printH(**kv)
     61                     schedule.every(interval).days.at(time).do(job_func, **kwargs)
     62         except Exception as e:
     63             raise e
     64 
     65     # 定义定时框架
     66     def schedule_timing(self, **kwargs):
     67         """
     68         :param kwargs: pyfilePath: "/home/agdwh/python_work/entity/compre.py"
     69                kwargs: report: "综合能率可视化"
     70         :return:
     71         """
     72         report = kwargs['report']
     73         time = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime('%Y-%m-%d')
     74         print(time + '日期' + report + "执行完成" + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
     75 
     76     # 統一啓動入口
     77     def run(self, joblist):
     78         # 将任务添加到schedule
     79         timing.doBySecond(timing.schedule_timing, joblist)
     80         timing.doByMinutes(timing.schedule_timing, joblist)
     81         timing.doByHours(timing.schedule_timing, joblist)
     82         # 开启无限循环
     83         while True:
     84             schedule.run_pending()
     85 
     86 
     87 if __name__ == "__main__":
     88     # 任务列表
     89     joblist = []
     90     """
     91     Rules for tasks:
     92     param:
     93         time: A string in one of the following formats: HH:MM:SS, HH:MM,:MM, :SS.
     94               Except doBySecond.
     95         schedule_type: Supports [second, mimute, hour, day].
     96         interval: The Interval for Program running.
     97         kwargs: Additional parameters for the report.
     98             
     99     e.g.
    100         {"schedule_type": "second", "interval": 10, "kwargs": {'pyfilePath': '/xxx/xxx.py', 'report': 'xxx'}}.
    101         {"time": ":18", "schedule_type": "minute", "interval": 5, "kwargs": {'pyfilePath': '/xxx/xxx.py', 'report': 'xxx'}}.
    102         {"time": ":58", "schedule_type": "hour", "interval": 1, "kwargs": {'pyfilePath': '/xxx/xxx.py', 'report': 'xxx'}}.
    103         {"time": "14:58:15", "schedule_type": "day", "interval": 1, "kwargs": {'pyfilePath': '/xxx/xxx.py', 'report': 'xxx'}}.
    104         
    105     """
    106     joblist.append({"time": ":18", "schedule_type": "minute", "interval": 1,
    107                     "kwargs": {'pyfilePath': '/home/agdwh/python_work/entity/compre7days.py', 'report': '综合能率可视化-mimute'}})
    108 
    109     joblist.append({"time": ":55", "schedule_type": "hour", "interval": 1,
    110                     "kwargs": {'pyfilePath': '/home/agdwh/python_work/entity/compre7days.py',
    111                                'report': '综合能率可视化-hour'}})
    112     joblist.append({"time": "14:54:15", "schedule_type": "day", "interval": 1,
    113                     "kwargs": {'pyfilePath': '/home/agdwh/python_work/entity/compre7days.py',
    114                                'report': '综合能率可视化-day'}})
    115     # 启动
    116     timing = Timing()
    117     timing.run(joblist)

    封裝了两层, 将任务列表定义层json 形式,其中又嵌入了一个参数key==>kwargs,作为内嵌函数的入参、

    程序执行结果:

     1 2020-03-01日期综合能率可视化-second执行完成2020-03-02 15:37:59
     2 2020-03-01日期综合能率可视化-second执行完成2020-03-02 15:38:09
     3 2020-03-01日期综合能率可视化-mimute执行完成2020-03-02 15:38:18
     4 2020-03-01日期综合能率可视化-second执行完成2020-03-02 15:38:19
     5 2020-03-01日期综合能率可视化-second执行完成2020-03-02 15:38:29
     6 2020-03-01日期综合能率可视化-second执行完成2020-03-02 15:38:39
     7 2020-03-01日期综合能率可视化-second执行完成2020-03-02 15:38:49
     8 2020-03-01日期综合能率可视化-second执行完成2020-03-02 15:38:59
     9 2020-03-01日期综合能率可视化-hour执行完成2020-03-02 15:39:00
    10 2020-03-01日期综合能率可视化-second执行完成2020-03-02 15:39:09
    11 2020-03-01日期综合能率可视化-mimute执行完成2020-03-02 15:39:18
    12 2020-03-01日期综合能率可视化-second执行完成2020-03-02 15:39:19
    欢迎对it热情的同学,加qq进行技术讨论; QQ:850922253
  • 相关阅读:
    iOS企业证书开发的APP证书过期时间监控
    事件冒泡,事件捕获
    倒计时
    获取多个div,点击第几个,显示第几个
    js继承
    javascript基础知识总结
    大型web系统高效应用方法(转载)
    数据库(内联,外联,交叉联)
    .net零碎基础知识点不完全小结
    C#的内存管理:堆、栈、托管堆与指针(转)
  • 原文地址:https://www.cnblogs.com/zhangwensi/p/12395874.html
Copyright © 2011-2022 走看看