python ray定时任务_python定时任务APScheduler
例如每隔五秒執(zhí)行一次:
import time
from apscheduler.schedulers.blocking import BlockingScheduler
def my_job():
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
#### 采用interval固定間隔模式,每隔五秒只執(zhí)行一次
sched.add_job(my_job, 'interval', seconds=5)
sched.start()
間隔調(diào)度,參數(shù)如下:
weeks (int) – 間隔幾周
days (int) – 間隔幾天
hours (int) – 間隔幾小時(shí)
minutes (int) – 間隔幾分鐘
seconds (int) – 間隔多少秒
start_date (datetime|str) – 開始日期
end_date (datetime|str) – 結(jié)束日期
timezone (datetime.tzinfo|str) – 時(shí)區(qū)
定時(shí)調(diào)度(例如在每一天上午八點(diǎn)半或者12點(diǎn)半執(zhí)行任務(wù))
import time
from apscheduler.schedulers.blocking import BlockingScheduler
scheduler = BlockingScheduler()
def everyday_crawler_job():
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
# subprocess.call("python Crawler.py")
sched = BlockingScheduler()
#每隔一天 執(zhí)行抓包程序
# sched.add_job(everyday_crawler_job, 'interval', days=1)days
#每天早上八點(diǎn)半和十二點(diǎn)半各執(zhí)行一次抓包程序
sched.add_job(everyday_crawler_job, 'cron', hour='8, 12', minute='30')
sched.start()
參數(shù)如下:
(int|str) 表示參數(shù)既可以是int類型,也可以是str類型
(datetime | str) 表示參數(shù)既可以是datetime類型,也可以是str類型
year (int|str) – 4-digit year -(表示四位數(shù)的年份,如2008年)
month (int|str) – month (1-12) -(表示取值范圍為1-12月)
day (int|str) – day of the (1-31) -(表示取值范圍為1-31日)
week (int|str) – ISO week (1-53) -(格里歷2006年12月31日可以寫成2006年-W52-7(擴(kuò)展形式)或2006W527(緊湊形式))
day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun) - (表示一周中的第幾天,既可以用0-6表示也可以用其英語(yǔ)縮寫表示)
hour (int|str) – hour (0-23) - (表示取值范圍為0-23時(shí))
minute (int|str) – minute (0-59) - (表示取值范圍為0-59分)
second (int|str) – second (0-59) - (表示取值范圍為0-59秒)
start_date (datetime|str) – earliest possible date/time to trigger on (inclusive) - (表示開始時(shí)間)
end_date (datetime|str) – latest possible date/time to trigger on (inclusive) - (表示結(jié)束時(shí)間)
timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone) -(表示時(shí)區(qū)取值)
總結(jié)
以上是生活随笔為你收集整理的python ray定时任务_python定时任务APScheduler的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CPU单挑到群架发展史
- 下一篇: 有意思的BAT CMD if while