Scheduling a Python script to run every 1 hr (details in description)

Question:

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?

Asked By: nYuker_98 D

||

Answers:

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()
Answered By: di.bezrukov

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 check this


For Windows:

Checkout Task Scheduler-

Press Win+R and type in taskschd.msc

Then click Create Task

Set a trigger for every 1 hour:

enter image description here

And set an action to run the script:

enter image description here

This should do the job

Answered By: Burritoos
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.