日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python教程:apscheduler模块使用教程

發(fā)布時(shí)間:2025/3/20 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python教程:apscheduler模块使用教程 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.簡介

apscheduler是python中的任務(wù)定時(shí)模塊,它包含四個(gè)組件:觸發(fā)器(trigger),作業(yè)存儲(chǔ)(job store),執(zhí)行器(executor),調(diào)度器(scheduler).

2.安裝

pip install apscheduler

3.示例

from apscheduler.schedulers.blocking import BlockingScheduler#作業(yè)1 def my_job1():print ('hello world!')#作業(yè)2 def my_job2(name):print ('hello world,', name)# 每個(gè)五秒運(yùn)行一次函數(shù) sched = BlockingScheduler() #不帶參數(shù)和和帶有參數(shù)的函數(shù) sched.add_job(my_job1, 'interval', seconds=5) sched.add_job(func=my_job2, args=('tom',), trigger='interval', seconds=5) sched.start()

4.講解

關(guān)于觸發(fā)器(trigger),它有三種參數(shù)可選:date / interval / cron.

date:一次性任務(wù),即只執(zhí)行一次任務(wù)。

參數(shù)如下:

next_run_time (datetime|str) – the date/time to run the job attimezone (datetime.tzinfo|str) – time zone for run_date if it doesn’t have one already

示例如下:

# 延時(shí)五秒后執(zhí)行一次 sched.add_job(func=my_job2, args=('tom',), trigger='date', next_run_time=now+datetime.timedelta(seconds=5))

interval:循環(huán)任務(wù),即按照時(shí)間間隔執(zhí)行任務(wù)。

參數(shù)如下:

weeks (int) – number of weeks to wait days (int) – number of days to wait hours (int) – number of hours to wait minutes (int) – number of minutes to wait seconds (int) – number of seconds to wait start_date (datetime|str) – starting point for the interval calculation end_date (datetime|str) – latest possible date/time to trigger on timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations

示例如下:

#每隔五秒執(zhí)行一次任務(wù) sched.add_job(func=my_job2, args=('tom',), trigger='interval', seconds=5)

cron:定時(shí)任務(wù),即在每個(gè)時(shí)間段執(zhí)行任務(wù)。

參數(shù)如下:

''' 學(xué)習(xí)中遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:857662006 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書! ''' year (int|str)4-digit year month (int|str) – month (1-12) day (int|str) – day of the (1-31) week (int|str) – ISO week (1-53) day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun) hour (int|str) – hour (0-23) minute (int|str) – minute (0-59) second (int|str) – second (0-59) start_date (datetime|str) – earliest possible date/time to trigger on (inclusive) end_date (datetime|str) – latest possible date/time to trigger on (inclusive) timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)


示例如下:

#在1-3,8-10月,每天的下午5點(diǎn),每一分鐘執(zhí)行一次任務(wù) sched.add_job(func=my_job1, trigger='cron', month='1-3,8-10', day='*', hour='17', minute='*')

總結(jié)

以上是生活随笔為你收集整理的python教程:apscheduler模块使用教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。