模块 schedule 定时任务
schedule模块实现定时任务
2018-08-29 15:01:51 更多
一、官方示例
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).days.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
while True:
schedule.run_pending()
time.sleep(1)
二、多线程解决多任务串行执行任务的延迟问题
未使用多线程
import datetime
import schedule
import time
def job1():
print("I'm working for job1")
time.sleep(2)
print("job1:", datetime.datetime.now())
def job2():
print("I'm working for job2")
time.sleep(2)
print("job2:", datetime.datetime.now())
def run():
schedule.every(10).seconds.do(job1)
schedule.every(10).seconds.do(job2)
while True:
schedule.run_pending()
time.sleep(1)
使用多线程
import datetime
import schedule
import threading
import time
def job1():
print("I'm working for job1")
time.sleep(2)
print("job1:", datetime.datetime.now())
def job2():
print("I'm working for job2")
time.sleep(2)
print("job2:", datetime.datetime.now())
def job1_task():
threading.Thread(target=job1).start()
def job2_task():
threading.Thread(target=job2).start()
def run():
schedule.every(10).seconds.do(job1_task)
schedule.every(10).seconds.do(job2_task)
while True:
schedule.run_pending()
time.sleep(1)
作者:ham731
个性签名:梦想要有的,万一实现了呢!
getee : https://gitee.com/cw731
gethub : https://github.com/cw731
gethub : https://github.com/cw731
博客园: https://www.cnblogs.com/ham-731/
简书 (已停止更新)https://www.jianshu.com/u/20d448d19898
简书 (已停止更新)https://www.jianshu.com/u/20d448d19898

浙公网安备 33010602011771号