python ray定时任务_python定时任务APScheduler
例如每隔五秒執行一次:
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固定間隔模式,每隔五秒只執行一次
sched.add_job(my_job, 'interval', seconds=5)
sched.start()
間隔調度,參數如下:
weeks (int) – 間隔幾周
days (int) – 間隔幾天
hours (int) – 間隔幾小時
minutes (int) – 間隔幾分鐘
seconds (int) – 間隔多少秒
start_date (datetime|str) – 開始日期
end_date (datetime|str) – 結束日期
timezone (datetime.tzinfo|str) – 時區
定時調度(例如在每一天上午八點半或者12點半執行任務)
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()
#每隔一天 執行抓包程序
# sched.add_job(everyday_crawler_job, 'interval', days=1)days
#每天早上八點半和十二點半各執行一次抓包程序
sched.add_job(everyday_crawler_job, 'cron', hour='8, 12', minute='30')
sched.start()
參數如下:
(int|str) 表示參數既可以是int類型,也可以是str類型
(datetime | str) 表示參數既可以是datetime類型,也可以是str類型
year (int|str) – 4-digit year -(表示四位數的年份,如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(擴展形式)或2006W527(緊湊形式))
day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun) - (表示一周中的第幾天,既可以用0-6表示也可以用其英語縮寫表示)
hour (int|str) – hour (0-23) - (表示取值范圍為0-23時)
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) - (表示開始時間)
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) -(表示時區取值)
總結
以上是生活随笔為你收集整理的python ray定时任务_python定时任务APScheduler的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CPU单挑到群架发展史
- 下一篇: python进阶路线 知乎_Python