Home > Net >  How to run scheduled task only once in Python
How to run scheduled task only once in Python

Time:01-11

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. I started code at 11:01. So for 60 seconds I got 6 results.

For second scheduled task 120 seconds passed since the beginning so I got 12 results.

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. This is why I placed schedule inside the while loop.

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)

enter image description here

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

CodePudding user response:

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.

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)

CodePudding user response:

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.

In this example you want task1 to be executed at 3 different times of the day and task 2 at times of the day

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.

This code will create the schedules for each task once for each element in your schedule list.

CodePudding user response:

Since your calls to schedule are inside while loop, they get placed on schedule multiple times. 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.

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).

  • Related