Search code examples
pythontaskjobsschedulehour

Run scheduled task only once on determinated hour in Python using 'schedule'


I want to execute a scheduled task to run only once with the library schedule in python

The example i have is this:

schedule.every().day.at(hour).do(job)

It execute a task every day, but i do not want it to execute every day, just once, something like this:

schedule.at(hour).do(job)

Is this possible?

EDIT

This is my code for better clarification

for hour in sorted(list_of_hours):
    schedule.every().day.at(hour).do(task)

Solution

  • The solution of the problem, i think was not the best pratice but works well, making a counter that go througt the task, and ends comparing the quantity of hours in te list and the counter, like this:

    
    counter = 0
    qtOfHours = 0
    
    def task():
        global counter += 1
    
    for hour in sorted(list_of_hours):
        schedule.every().day.at(hour).do(task)
    
    while True:
        schedule.run_pending()
        time.sleep(1)
        if counter == qtOfHours:
            sys.exit()
    

    Then to run it every day, i will use OS based tools to schedule the script, just like the answers before, thanks