python定時(shí)任務(wù)schedule庫用法詳細(xì)講解
前言
schedule是一個第三方輕量級的任務(wù)調(diào)度模塊,可以按照秒,分,小時(shí),日期或者自定義事件執(zhí)行時(shí)間。
如果想執(zhí)行多個任務(wù),也可以添加多個task。
首先安裝schedule庫:
pip install schedule

1、按時(shí)間間隔執(zhí)行定時(shí)任務(wù)
示例代碼1:
import schedule
from datetime import datetime
def task():
now = datetime.now()
ts = now.strftime("%Y-%m-%d %H:%M:%S")
print(ts)
def task2():
now = datetime.now()
ts = now.strftime("%Y-%m-%d %H:%M:%S")
print(ts + '666!')
def func():
# 清空任務(wù)
schedule.clear()
# 創(chuàng)建一個按3秒間隔執(zhí)行任務(wù)
schedule.every(3).seconds.do(task)
# 創(chuàng)建一個按2秒間隔執(zhí)行任務(wù)
schedule.every(2).seconds.do(task2)
while True:
schedule.run_pending()
func()運(yùn)行結(jié)果:

示例代碼2:
import schedule
import time
def job(name):
print("her name is : ", name)
name = "張三"
schedule.every(10).minutes.do(job, name)
schedule.every().hour.do(job, name)
schedule.every().day.at("10:30").do(job, name)
schedule.every(5).to(10).days.do(job, name)
schedule.every().monday.do(job, name)
schedule.every().wednesday.at("13:15").do(job, name)
while True:
schedule.run_pending()
time.sleep(1)參數(shù)解釋:
- 每隔十分鐘執(zhí)行一次任務(wù)
- 每隔一小時(shí)執(zhí)行一次任務(wù)
- 每天的10:30執(zhí)行一次任務(wù)
- 每隔5到10天執(zhí)行一次任務(wù)
- 每周一的這個時(shí)候執(zhí)行一次任務(wù)
- 每周三13:15執(zhí)行一次任務(wù)
- run_pending:運(yùn)行所有可以運(yùn)行的任務(wù)
注意:schedule方法是串行的,也就是說,如果各個任務(wù)之間時(shí)間不沖突,那是沒問題的;如果時(shí)間有沖突的話,會串行的執(zhí)行命令。
2、裝飾器:通過 @repeat() 裝飾靜態(tài)方法
示例代碼:
from datetime import datetime
from schedule import every, repeat, run_pending
@repeat(every(3).seconds)
def task():
now = datetime.now()
ts = now.strftime("%Y-%m-%d %H:%M:%S")
print(ts + '-333!')
@repeat(every(5).seconds)
def task2():
now = datetime.now()
ts = now.strftime("%Y-%m-%d %H:%M:%S")
print(ts + "-555555!")
while True:
run_pending()運(yùn)行結(jié)果:

3、傳遞參數(shù)
示例代碼:
from datetime import datetime
import schedule
def task(s):
now = datetime.now()
ts = now.strftime("%Y-%m-%d %H:%M:%S")
print(ts + s)
def task2(s):
now = datetime.now()
ts = now.strftime("%Y-%m-%d %H:%M:%S")
print(ts + s)
schedule.every(3).seconds.do(task, s='-333')
schedule.every(5).seconds.do(task, s='-555')
while True:
schedule.run_pending()運(yùn)行結(jié)果:

4、使用裝飾器傳遞參數(shù)
示例代碼:
from datetime import datetime
from schedule import every, repeat, run_pending
@repeat(every(3).seconds, '-333')
def task(s):
now = datetime.now()
ts = now.strftime("%Y-%m-%d %H:%M:%S")
print(ts + s)
@repeat(every(5).seconds, '-555')
def task2(s):
now = datetime.now()
ts = now.strftime("%Y-%m-%d %H:%M:%S")
print(ts + s)
while True:
run_pending()運(yùn)行結(jié)果:

5、取消定時(shí)任務(wù)
示例代碼:
import schedule
i = 0
def some_task():
global i
i += 1
print(i)
if i == 5:
schedule.cancel_job(job)
print('cancel job')
exit(0)
job = schedule.every().second.do(some_task)
while True:
schedule.run_pending()運(yùn)行結(jié)果:

6、在指定時(shí)間執(zhí)行一次任務(wù)
示例代碼:
import time
import schedule
def job_that_executes_once():
print('Hello')
return schedule.CancelJob
schedule.every().minute.at(':30').do(job_that_executes_once)
while True:
schedule.run_pending()
time.sleep(1)運(yùn)行結(jié)果:

7、根據(jù)標(biāo)簽檢索任務(wù)
示例代碼:
# 檢索所有任務(wù):schedule.get_jobs()
import schedule
def greet(name):
print('Hello {}'.format(name))
schedule.every().day.do(greet, 'Andrea').tag('daily-tasks', 'friend')
schedule.every().hour.do(greet, 'John').tag('hourly-tasks', 'friend')
schedule.every().hour.do(greet, 'Monica').tag('hourly-tasks', 'customer')
schedule.every().day.do(greet, 'Derek').tag('daily-tasks', 'guest')
friends = schedule.get_jobs('friend')
print(friends)運(yùn)行結(jié)果:

8、根據(jù)標(biāo)簽取消任務(wù)
示例代碼:
# 取消所有任務(wù):schedule.clear()
import schedule
def greet(name):
print('Hello {}'.format(name))
if name == 'Cancel':
schedule.clear('second-tasks')
print('cancel second-tasks')
schedule.every().second.do(greet, 'Andrea').tag('second-tasks', 'friend')
schedule.every().second.do(greet, 'John').tag('second-tasks', 'friend')
schedule.every().hour.do(greet, 'Monica').tag('hourly-tasks', 'customer')
schedule.every(5).seconds.do(greet, 'Cancel').tag('daily-tasks', 'guest')
while True:
schedule.run_pending()運(yùn)行結(jié)果:

9、運(yùn)行任務(wù)到某時(shí)間
示例代碼:
import schedule
from datetime import datetime, timedelta, time
def job():
print('working...')
schedule.every().second.until('23:59').do(job) # 今天23:59停止
schedule.every().second.until('2030-01-01 18:30').do(job) # 2030-01-01 18:30停止
schedule.every().second.until(timedelta(hours=8)).do(job) # 8小時(shí)后停止
schedule.every().second.until(time(23, 59, 59)).do(job) # 今天23:59:59停止
schedule.every().second.until(datetime(2030, 1, 1, 18, 30, 0)).do(job) # 2030-01-01 18:30停止
while True:
schedule.run_pending()運(yùn)行結(jié)果:

10、馬上運(yùn)行所有任務(wù)(主要用于測試)
示例代碼:
import schedule
def job():
print('working...')
def job1():
print('Hello...')
schedule.every().monday.at('12:40').do(job)
schedule.every().tuesday.at('16:40').do(job1)
schedule.run_all()
schedule.run_all(delay_seconds=3) # 任務(wù)間延遲3秒運(yùn)行結(jié)果:

11、并行運(yùn)行:使用 Python 內(nèi)置隊(duì)列實(shí)現(xiàn)
示例代碼:
import threading
import time
import schedule
def job1():
print("I'm running on thread %s" % threading.current_thread())
def job2():
print("I'm running on thread %s" % threading.current_thread())
def job3():
print("I'm running on thread %s" % threading.current_thread())
def run_threaded(job_func):
job_thread = threading.Thread(target=job_func)
job_thread.start()
schedule.every(10).seconds.do(run_threaded, job1)
schedule.every(10).seconds.do(run_threaded, job2)
schedule.every(10).seconds.do(run_threaded, job3)
while True:
schedule.run_pending()
time.sleep(1)運(yùn)行結(jié)果:

總結(jié)
到此這篇關(guān)于python定時(shí)任務(wù)schedule庫用法的文章就介紹到這了,更多相關(guān)python定時(shí)任務(wù)schedule庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python自動定時(shí)任務(wù)schedule庫的使用方法
- Python apscheduler實(shí)現(xiàn)定時(shí)任務(wù)的方法詳解
- Python高效定時(shí)任務(wù)處理APScheduler庫深入學(xué)習(xí)
- Python?Apschedule定時(shí)任務(wù)框架的用法詳解
- Python第三方模塊apscheduler安裝和基本使用
- Python flask框架定時(shí)任務(wù)apscheduler應(yīng)用介紹
- Python中schedule模塊關(guān)于定時(shí)任務(wù)使用方法
- Python定時(shí)任務(wù)框架APScheduler安裝使用詳解
- 最新Python?APScheduler?定時(shí)任務(wù)詳解
- Python中schedule擴(kuò)展的具體使用
相關(guān)文章
基于Python實(shí)現(xiàn)五子棋-(人機(jī)對戰(zhàn))
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)五子棋游戲(人機(jī)對戰(zhàn)版),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-05-05
Python如何操作office實(shí)現(xiàn)自動化及win32com.client的運(yùn)用
這篇文章主要介紹了Python如何操作office實(shí)現(xiàn)自動化及win32com.client的運(yùn)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python中print和return的作用及區(qū)別解析
print的作用是輸出數(shù)據(jù)到控制端,就是打印在你能看到的界面上。這篇文章給大家介紹Python中print和return的作用及區(qū)別解析,感興趣的朋友跟隨小編一起看看吧2019-05-05
python3實(shí)現(xiàn)網(wǎng)頁版raspberry pi(樹莓派)小車控制
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)網(wǎng)頁版raspberry pi(樹莓派)小車控制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
requests.post()方法中data和json參數(shù)的使用
這篇文章主要介紹了requests.post()方法中data和json參數(shù)的使用方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

