简体   繁体   English

如何在Python中只运行一次计划任务

[英]How to run scheduled task only once in Python

I am trying to run scheduled tasks only once at the specific time but I couldn't figure out how to do that.我试图在特定时间只运行一次计划任务,但我不知道该怎么做。 All I understand is, every 10 seconds 1 task is stored and then when the time comes job runs them all.(I shared the result in below.) For example test_1 task scheduled to 11:02.我所了解的是,每 10 秒存储 1 个任务,然后当时间到来时作业运行它们。(我在下面分享了结果。)例如 test_1 任务安排在 11:02。 I started code at 11:01.我在 11:01 开始编写代码。 So for 60 seconds I got 6 results.所以在 60 秒内我得到了 6 个结果。

For second scheduled task 120 seconds passed since the beginning so I got 12 results.对于第二个计划任务,从一开始就过去了 120 秒,所以我得到了 12 个结果。

Sorry for the misinformation.对于错误信息,我们深表歉意。 I updated my question.我更新了我的问题。 I also shared my excel date list I hope this gives more information.If today is work day (1) then run scheduled tasks else do nothing.This code will run nonstop so I have to check the current date.我还分享了我的 excel 日期列表,希望这能提供更多信息。如果今天是工作日 (1),则运行计划任务,否则什么都不做。此代码将不间断运行,因此我必须检查当前日期。 This is why I placed schedule inside the while loop.这就是为什么我将 schedule 放在 while 循环中的原因。

import pandas as pd
from datetime import datetime
import time
import schedule 

def test_1():
    print("do task 1")

def test_2():
    print("do task 2")

while(1 != 2):

     today = datetime.today().strftime('%Y/%m/%d')

     df_Calender = pd.read_excel('Calender.xlsx', sheet_name=0) 

     date = df_Calender['Date'] #Date column

     filter_1 = date == today
     df1_Calender = df_Calender.loc[filter_1]

     week_day = df1_Calender['WeekDays']

     if (week_day.item() != 1):
        print("do nothing")
     else:
        schedule.every().day.at("11:02").do(test_1)
        schedule.every().day.at("11:03").do(test_2)

        time.sleep(10)

        schedule.run_pending()

Here is the date list(Calender.xlsx)这是日期列表(Calender.xlsx)

在此处输入图像描述

This is the result这是结果

do task 1
do task 1
do task 1
do task 1
do task 1
do task 1
do task 2
do task 2
do task 2
do task 2
do task 2
do task 2
do task 2
do task 2
do task 2
do task 2
do task 2
do task 2

You would want to move your schedule out of the while loop, otherwise, it will keep scheduling the task after every 10-seconds due to the time.sleep(10) in the endless loop.您可能希望将您的schedule移出while循环,否则,由于无限循环中的time.sleep(10) ,它会在每 10 秒后继续安排任务。

You can try something like:你可以尝试这样的事情:

def task1():
   # Do something

schedule.every().day.at("11:02").do(task1)

# If you only want it to run once, then no loop is required
# Else if you want it to run once per day then add the while True loop
schedule.run_pending()
time.sleep(1)

Not sure if this is what you want.不确定这是否是您想要的。 But it's what I understood you want但这就是我所理解的你想要的

You have a list of times when you want a certain function to be called.您有一个时间列表,希望某个 function 被调用。

In this example you want task1 to be executed at 3 different times of the day and task 2 at times of the day在此示例中,您希望在一天中的 3 个不同时间执行任务 1,并在一天中的不同时间执行任务 2

task1_schedule = ["11:02", "13:30", "17:00"]
task2_schedule = ["11:03", "14:20"]

Then you just iterate over the list and set a schedule然后您只需遍历列表并设置时间表

for start_time in task1_schedule:
    schedule.every().day.at(start_time).do(test_1)

for start_time in task2_schedule:
    schedule.every().day.at(start_time).do(test_2)

In this case since there are only 2 different taks I made 2 different list.在这种情况下,因为只有 2 个不同的任务,所以我制作了 2 个不同的列表。

This code will create the schedules for each task once for each element in your schedule list.此代码将为计划列表中的每个元素创建一次每个任务的计划。

Since your calls to schedule are inside while loop, they get placed on schedule multiple times.由于您对 schedule 的调用在 while 循环内,因此它们会多次按计划进行。 You need to place items on schedule once and only check if they are scheduled in loop.您需要将项目按计划放置一次,并且只检查它们是否按循环计划。 Try moving both schedule lines in front of while loop.尝试将两个计划行移动到 while 循环之前。

from datetime import datetime
import time
import schedule 

def test_1():
   print("do task 1")

def test_2():
   print("do task 2")

schedule.every().day.at("11:02").do(test_1)
schedule.every().day.at("11:03").do(test_2)

while(1 != 2):
   time.sleep(10)

   schedule.run_pending()

See this example in documentation.请参阅文档中的此示例


Based on comment you also need to check date before running task.根据评论,您还需要在运行任务之前检查日期。 I advise not to schedule conditionally, but to check for date either inside test_1 and test_2 functions or specify more day/week conditions (for example .every(3).days for each third day).我建议不要有条件地安排,而是在test_1test_2函数中检查日期或指定更多的日/周条件(例如.every(3).days每三天)。

I have found the solution.我找到了解决方案。 Just adding schedule.clear() after schedule.run_pending() clears all stored tasks and leaves only one scheduled job.只需在schedule.clear() schedule.run_pending()即可清除所有存储的任务并仅留下一个计划作业。 Thank you for your help.谢谢您的帮助。

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

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