简体   繁体   中英

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). 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?

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()

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:

在此处输入图像描述

And set an action to run the script:

在此处输入图像描述

This should do the job

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM