zoukankan      html  css  js  c++  java
  • python中schedule模块的使用

    使用python进行job管理的schedule模块,简单好用,在这里记录一下。
    详细源码可以参考这里  https://github.com/dbader/schedule

    安装方法
        pip install schedule


    使用方法

    import schedule
    import time
     
    def test():
        print("I'm working...")
    def test2():    
        print("I'm working... in job2")
     
    # 每10分钟执行一次job函数
    schedule.every(10).minutes.do(test)
    # 每10秒执行一次job函数
    schedule.every(10).seconds.do(test)
    # 当every()没参数时默认是1小时/分钟/秒执行一次job函数
    schedule.every().hour.do(test)
    schedule.every().day.at("10:30").do(test)
    schedule.every().monday.do(test)
    # 具体某一天某个时刻执行一次job函数
    schedule.every().wednesday.at("13:15").do(test)
    # 可以同时定时执行多个任务,但是每个任务是按顺序执行
    schedule.every(10).seconds.do(job2)
    # 如果job函数有有参数时,这么写
    schedule.every(10).seconds.do(job,"参数")
    while True:
        # 启动服务
        schedule.run_pending()
        time.sleep(1)

    运行该程序之后,可以定时的进行执行。除了代码中提到的方法之外,还有例如seconds等的一些方法,可以参考源码。

    TIPS

    注意,schedule方法是串行的,也就是说,如果各个任务之间时间不冲突,那是没问题的;如果时间有冲突的话,会串行的执行命令,例如以下代码

     1 import schedule
     2 import time
     3 import threading
     4 
     5 def job():
     6     print("I'm working... in job1  start")
     7     time.sleep(15)
     8     print("I'm working... in job1  end")
     9 
    10 def job2():
    11     print("I'm working... in job2")
    12 
    13 schedule.every(10).seconds.do(job)
    14 schedule.every(10).seconds.do(job2)
    15 
    16 while True:
    17     schedule.run_pending()
    18     time.sleep(1)

    输出如下:


        I’m working… in job1 start
        I’m working… in job1 end
        I’m working… in job2

    可以改成多线程的方式:

     1 import schedule
     2 import time
     3 import threading
     4 
     5 def job():
     6     print("I'm working... in job1  start")
     7     time.sleep(15)
     8     print("I'm working... in job1  end")
     9 
    10 def job2():
    11     print("I'm working... in job2")
    12 
    13 def run_threaded(job_func):
    14      job_thread = threading.Thread(target=job_func)
    15      job_thread.start()
    16        #threading.Thread(target=loop,args=(i,loops[i]))
    17 
    18  schedule.every(10).seconds.do(run_threaded,job)
    19  schedule.every(10).seconds.do(run_threaded,job2)
    20 
    21 
    22 while True:
    23     schedule.run_pending()
    24     time.sleep(1)

    有如下输出:


        I’m working… in job1 start
        I’m working… in job2
        I’m working… in job1 start
        I’m working… in job2
        I’m working… in job1 end

    可见在并行的执行。
    如果想要对线程的数量有所控制,“If you want tighter control on the number of threads use a shared jobqueue and one or more worker threads”,则可以采用如下方法:

     1 import Queue
     2 import time
     3 import threading
     4 import schedule
     5 
     6 
     7 def job():
     8     print("I'm working")
     9 
    10 
    11 def worker_main():
    12     while 1:
    13         job_func = jobqueue.get()
    14         job_func()
    15 
    16 jobqueue = Queue.Queue()
    17 
    18 schedule.every(10).seconds.do(jobqueue.put, job)
    19 schedule.every(10).seconds.do(jobqueue.put, job)
    20 schedule.every(10).seconds.do(jobqueue.put, job)
    21 schedule.every(10).seconds.do(jobqueue.put, job)
    22 schedule.every(10).seconds.do(jobqueue.put, job)
    23 
    24 worker_thread = threading.Thread(target=worker_main)
    25 worker_thread.start()
    26 
    27 while 1:
    28     schedule.run_pending()
    29     time.sleep(1)
  • 相关阅读:
    封装React AntD的dialog弹窗组件
    封装Vue Element的dialog弹窗组件
    封装Vue Element的upload上传组件
    封装Vue Element的form表单组件
    Kubernetes实战总结
    【转载】kubelet 参数详解
    Kubernetes实战总结
    【解决】 使用Grafana查看k8s集群监控情况,提示插件未发现和没有数据
    Ingress-nginx 与 Nginx-ingress
    【解决】Error from server (ServiceUnavailable): the server is currently unable to handle the request
  • 原文地址:https://www.cnblogs.com/math98/p/8780563.html
Copyright © 2011-2022 走看看