天天看點

Python定時任務sched(一)

這裡介紹一下python中定時任務:sched

python中自帶的是sched,也可以通過pip下載下傳schedule進行任務定時處理,這裡先簡單介紹下sched的使用

import datetime
import schedule
import time
import sched

schedule2=sched.scheduler(time.time,time.sleep)

def fun2(string1):
    # time.sleep(10)
    print("now is ",time.time(),"====",string1,"==string2=")

def fun3(string1):
    # time.sleep(10)
    print("now is ",time.time(),"====",string1,"==fun3=")

def fun4(string1):
    # time.sleep(10)
    print("now is ",time.time(),"====",string1,"==fun4=")

def run2():
    # print(1)
    # enter之後可帶的參數意義:delay, priority, action, argument=()
    # delay表示執行周期,也就是多久之後開始執行
    # priority表示執行優先等級,1~10的優先級排序,1為最先執行者
    # action執行的函數名
    # argument表示函數帶的參數,以{}形式封裝
    schedule2.enter(1, 1, fun2,{"goods1"})
    schedule2.enter(1, 2, fun2,{"goods2"})
    schedule2.enter(1, 3, fun2,{"goods3"})
    schedule2.enter(1, 4, fun3, {"goods4"})
    schedule2.enter(1, 5, fun4, {"goods5"})
    schedule2.enter(1, 6, fun3, {"goods6"})
    schedule2.enter(1, 7, fun4, {"goods7"})
    schedule2.run()