1. 程式人生 > >python 定時任務schedule

python 定時任務schedule

step 1 # 準備好任務 ,假設我們要定時執行的任務是sayhello函式
test.py

def sayhello(name):
    print('hello time scheduler'+name)


if __name__ == '__main__':
    sayhello()

step 2 # 任務函式交給schedule定時執行
runtask.py

import schedule
import time
from test import sayhello
# 00:30
# schedule.every().day.at(""00:30"").do(sayhello,name='parhat')
schedule.every().second.do(sayhello,name='parhat')

while True:
    schedule.run_pending()
    time.sleep(1)

注:需要安裝schedule包

pip install schedule

Appedex # 定時任務示例

schedule.every(10).minutes.do(sayhello)
schedule.every().hour.do(sayhello)
schedule.every().day.at("10:30").do(sayhello)
schedule.every(5).to(10).minutes.do(sayhello)
schedule.every().monday.do(sayhello)
schedule.every().wednesday.at("13:15").do(sayhello)