Home > Software design >  Automate API Request in Python
Automate API Request in Python

Time:09-20

everyone. Hope someone can help me.

I have an API connection and I want to request/get and store (atualize my csv where the data is located) data hourly without using task scheduler from Windows. Basically I want to schedule the python file to run at 6am, 7am, 8am, 9 and so on.

Do you know how I can do that?

CodePudding user response:

There is a python module that does exactly that: https://schedule.readthedocs.io/en/stable/

But please be aware of this chapter: https://schedule.readthedocs.io/en/stable/#when-not-to-use-schedule.

CodePudding user response:

One option is to do it with Rocketry:

from rocketry import Rocketry
from rocketry.conds import daily

app = Rocketry()

@app.task(daily.at("06:00") | daily.at("07:00") | daily.at("08:00") | daily.at("09:00"))
def process_data():
    ... # Do the task

if __name__ == "__main__":
    app.run()

But that gets tedious, it seems you want it to run hourly between certain time which can be also achieved by:

from rocketry import Rocketry
from rocketry.conds import hourly, time_of_day

app = Rocketry()

@app.task(hourly & time_of_day.between("06:00", "09:00"))
def process_data():
    ... # Do the task

if __name__ == "__main__":
    app.run()

Note: | is OR operator and & is AND operator. The first argument in the .task is a condition that is either True or False (if True, the task will start).

  • Related