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)
原文地址:https://www.cnblogs.com/ham-731/p/12121482.html
时间: 2024-10-29 19:42:15