[英]Scheduling a Python script to run every 1 hr (details in description)
My question is simple.我的问题很简单。 I have a Python script that pulls data from an API that has a limit of only 150 requests per hour (here 150 dates of data).
我有一个 Python 脚本,它从一个限制为每小时 150 个请求(这里是 150 个数据日期)的 API 中提取数据。 I have a total of 6 chunks (
6 * 150
) to pull.我总共有 6 个块(
6 * 150
)要拉。 So I am trying to schedule this script so it runs every 1 hour for 150 dates and sleeps for 1 hr.所以我试图安排这个脚本,让它每 1 小时运行一次,持续 150 个日期,然后休眠 1 小时。
How do I do it?我该怎么做?
I guess for schedule a Python script to run every hour, you can use time
module in the Python Standard Library.我猜想安排一个 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)
So your code will run the main()
function six times, with a 1 hour pause in between each run, and then exit the loop and terminate the script.因此,您的代码将运行
main()
函数六次,每次运行之间暂停 1 小时,然后退出循环并终止脚本。
Also you can use the schedule
module from the apscheduler
library您也可以使用
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()
check out crontab:查看 crontab:
run crontab -e
in the terminal and crontab will open a terminal editor, type:在终端中运行
crontab -e
,crontab 将打开一个终端编辑器,键入:
* 1 * * * python3 ~/full/path/to/script.py
save it and it should work.保存它,它应该可以工作。
for more information about crontab check this有关 crontab 的更多信息, 请检查此
Checkout Task Scheduler-结帐任务计划程序-
Press Win+R
and type in taskschd.msc
按
Win+R
并输入taskschd.msc
Then click Create Task
然后点击
Create Task
Set a trigger for every 1 hour:每 1 小时设置一次触发器:
And set an action to run the script:并设置一个动作来运行脚本:
This should do the job这应该做的工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.