简体   繁体   English

使用“计划”在 Python 中的确定时间仅运行一次计划任务

[英]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我想使用 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:问题的解决方案,我认为这不是最好的做法,但效果很好,制作了一个 go 完成任务的计数器,最后比较了 te 列表和计数器中的小时数,如下所示:


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 .您可以尝试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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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