[英]Scheduling a Python script to run every 1 hr (details in description)
我的問題很簡單。 我有一個 Python 腳本,它從一個限制為每小時 150 個請求(這里是 150 個數據日期)的 API 中提取數據。 我總共有 6 個塊( 6 * 150
)要拉。 所以我試圖安排這個腳本,讓它每 1 小時運行一次,持續 150 個日期,然后休眠 1 小時。
我該怎么做?
我猜想安排一個 Python 腳本每小時運行一次,您可以使用 Python 標准庫中的time
模塊。
import time
def main():
# code to pull data goes here
counter = 0
while True:
main()
counter += 1
if counter == 6:
break
time.sleep(3600)
因此,您的代碼將運行main()
函數六次,每次運行之間暫停 1 小時,然后退出循環並終止腳本。
您也可以使用apscheduler
庫中的schedule
模塊
from apscheduler.schedulers.blocking import BlockingScheduler
def main():
# code to pull data goes here
scheduler = BlockingScheduler()
scheduler.add_job(main, "interval", hours=1)
scheduler.start()
查看 crontab:
在終端中運行crontab -e
,crontab 將打開一個終端編輯器,鍵入:
* 1 * * * python3 ~/full/path/to/script.py
保存它,它應該可以工作。
有關 crontab 的更多信息, 請檢查此
結帳任務計划程序-
按Win+R
並輸入taskschd.msc
然后點擊Create Task
每 1 小時設置一次觸發器:
並設置一個動作來運行腳本:
這應該做的工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.