zoukankan      html  css  js  c++  java
  • 【Python学习】schedule模块构建定时任务

    python中有一个轻量级的定时任务调度的库:schedule。他可以完成每分钟,每小时,每天,周几,特定日期的定时任务。因此十分方便我们执行一些轻量级的定时任务.

    代码如下:

    复制代码
     1 import schedule
     2 import time
     3  
     4 def job(name):
     5     print("her name is : ", name)
     6  
     7 name = "longsongpong"
     8 schedule.every(10).minutes.do(job, name)
     9 schedule.every().hour.do(job, name)
    10 schedule.every().day.at("10:30").do(job, name)
    11 schedule.every(5).to(10).days.do(job, name)
    12 schedule.every().monday.do(job, name)
    13 schedule.every().wednesday.at("13:15").do(job, name)
    14  
    15 while True:
    16     schedule.run_pending()
    17     time.sleep(1)
    复制代码
    • 每隔十分钟执行一次任务
    • 每隔一小时执行一次任务
    • 每天的10:30执行一次任务
    • 每隔5到10天执行一次任务 
    • 每周一的这个时候执行一次任务
    • 每周三13:15执行一次任务
    • run_pending:运行所有可以运行的任务

    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  
    17  schedule.every(10).seconds.do(run_threaded,job)
    18  schedule.every(10).seconds.do(run_threaded,job2)
    19  
    20  
    21 while True:
    22     schedule.run_pending()
    23     time.sleep(1)
    复制代码

    结果如下:

    复制代码
    1 I'm working... in job1  start
    2 I'm working... in job2
    3 I'm working... in job1  start
    4 I'm working... in job2
    5 I'm working... in job1  end
    6 I'm working... in job1  start
    7 I'm working... in job2
    复制代码
    作者:gtea 博客地址:https://www.cnblogs.com/gtea
  • 相关阅读:
    安全编码1
    VPP tips
    VPP概述汇总
    C语言安全编码摘录
    TCP-proxy
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.4. Matplotlib: plotting
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.3. NumPy: creating and manipulating numerical data
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.2. The Python language
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.1. Python scientific computing ecosystem
    25马5跑道,求最快的五匹马的需要比赛的次数
  • 原文地址:https://www.cnblogs.com/gtea/p/13050953.html
Copyright © 2011-2022 走看看