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). I have a total of 6 chunks (6 * 150
) to pull. So I am trying to schedule this script so it runs every 1 hour for 150 dates and sleeps for 1 hr.
How do I do it?
CodePudding user response:
I guess for schedule a Python script to run every hour, you can use time
module in the Python Standard Library.
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.
Also you can use the schedule
module from the apscheduler
library
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()
CodePudding user response:
Update:
For MacOs:
check out crontab:
run crontab -e
in the terminal and
crontab will open a terminal editor, type:
* 1 * * * python3 ~/full/path/to/script.py
save it and it should work.
for more information about crontab
And set an action to run the script:
This should do the job