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)
  • 相关阅读:
    机器学习算法
    消息队列
    net core 静态文件
    Startup 和 Middleware(中间件)
    HLS 协议
    Web APIs 基于令牌TOKEN验证的实现
    cyq.data开源
    FluentConsole是一个托管在github的C#开源组件
    数据层全栈式编程架构
    Java NIO 完全学习笔记(转)
  • 原文地址:https://www.cnblogs.com/math98/p/8780563.html
Copyright © 2011-2022 走看看