python3 schedule模块实现定时任务

一、官方示例

import   schedule,  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, schedule,  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,  schedule,  threading,  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()

defjob2_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)

你可能感兴趣的:(python3 schedule模块实现定时任务)