欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python定時(shí)任務(wù)schedule庫用法詳細(xì)講解

 更新時(shí)間:2023年01月11日 11:01:09   作者:IT之一小佬  
python中有一個(gè)輕量級的定時(shí)任務(wù)調(diào)度的庫schedule,下面這篇文章主要給大家介紹了關(guān)于python定時(shí)任務(wù)schedule庫用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

schedule是一個(gè)第三方輕量級的任務(wù)調(diào)度模塊,可以按照秒,分,小時(shí),日期或者自定義事件執(zhí)行時(shí)間。

如果想執(zhí)行多個(gè)任務(wù),也可以添加多個(gè)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)建一個(gè)按3秒間隔執(zhí)行任務(wù)
    schedule.every(3).seconds.do(task)
    # 創(chuàng)建一個(gè)按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ù) 
  • 每周一的這個(gè)時(shí)候執(zhí)行一次任務(wù)
  • 每周三13:15執(zhí)行一次任務(wù)
  • run_pending:運(yùn)行所有可以運(yùn)行的任務(wù)

 注意:schedule方法是串行的,也就是說,如果各個(gè)任務(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Pycharm報(bào)錯(cuò):'NoneType'?object?has?no?attribute?'bytes'的解決方法

    Pycharm報(bào)錯(cuò):'NoneType'?object?has?no?attribute?

    這篇文章主要給大家介紹了關(guān)于Pycharm報(bào)錯(cuò):'NoneType'?object?has?no?attribute?'bytes'的解決方法,文中通過圖文將解決的方法介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • python中defaultdict方法的使用詳解

    python中defaultdict方法的使用詳解

    這篇文章主要介紹了python中defaultdict方法的使用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • python獲取100以內(nèi)的質(zhì)數(shù)3種方式總結(jié)

    python獲取100以內(nèi)的質(zhì)數(shù)3種方式總結(jié)

    質(zhì)數(shù)也稱為質(zhì)數(shù),具有無限個(gè),質(zhì)數(shù)定義為大于1的自然數(shù),除了1和它本省外沒有其他因數(shù)的數(shù)稱為質(zhì)數(shù),這篇文章主要給大家介紹了關(guān)于python獲取100以內(nèi)質(zhì)數(shù)的3種方式,需要的朋友可以參考下
    2024-08-08
  • 一篇文章帶你了解python標(biāo)準(zhǔn)庫--time模塊

    一篇文章帶你了解python標(biāo)準(zhǔn)庫--time模塊

    下面小編就為大家?guī)硪黄猵ython模塊之time模塊。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-08-08
  • python 刪除空值且合并excel的操作

    python 刪除空值且合并excel的操作

    這篇文章主要介紹了python 刪除空值且合并excel的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • 如何在VSCode下使用Jupyter的教程詳解

    如何在VSCode下使用Jupyter的教程詳解

    這篇文章主要介紹了如何在VSCode下使用Jupyter,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 3行Python代碼實(shí)現(xiàn)圖像照片摳圖和換底色的方法

    3行Python代碼實(shí)現(xiàn)圖像照片摳圖和換底色的方法

    這篇文章主要介紹了3行Python代碼實(shí)現(xiàn)圖像照片摳圖和換底色的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Python 正則 re.compile 真的必需嗎

    Python 正則 re.compile 真的必需嗎

    本文主要介紹了Python 正則 re.compile 真的必需嗎,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 用Python實(shí)現(xiàn)數(shù)據(jù)的透視表的方法

    用Python實(shí)現(xiàn)數(shù)據(jù)的透視表的方法

    今天小編就為大家分享一篇用Python實(shí)現(xiàn)數(shù)據(jù)的透視表的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python scrapy爬取起點(diǎn)中文網(wǎng)小說榜單

    Python scrapy爬取起點(diǎn)中文網(wǎng)小說榜單

    爬蟲的基礎(chǔ)內(nèi)容已經(jīng)全部學(xué)玩,博主決定想著更加標(biāo)準(zhǔn)化以及實(shí)用能力更強(qiáng)的scrapy進(jìn)發(fā),今天記錄自己第一個(gè)scrapy爬蟲項(xiàng)目. scrapy爬取起點(diǎn)中文網(wǎng)24小時(shí)熱銷榜單,需要的朋友可以參考下
    2021-06-06

最新評論