简体   繁体   中英

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)

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

You can try simple_scheduler .

from simple_scheduler.event import event_scheduler
event_scheduler.timezones()
TZ = "Asia/Kolkata"
WHEN = ["mon|10:00", "tue|11:30", "wed|14:00"]
def target(a, b=1, *args, **kwargs):
    print(a, b, args, kwargs)

event_scheduler.add_job(target = target,
                        args = (0,), # don't forget "," for single arguments
                        kwargs = {"b":2},
                        when = WHEN,
                        tz = TZ)
event_scheduler.run()

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