Home > Enterprise >  Start a Function at a specific time range
Start a Function at a specific time range

Time:05-26

I need to run a specific funtion every 5 minutes, starting on Sunday night at 11pm until Friday 11pm. How can I do it? I tried with "schedule" module, below a general function that print the instant time

import time
from datetime import datetime

def script_sample():
  now = datetime.now()
  print(now)

schedule.every(5).minutes.sunday.at("23:00").until.friday.at("23:00").do(script_sample)
while True:
  schedule.run_pending()

But I receive the following error:

AttributeError: 'function' object has no attribute 'friday'

How can I do it? Is "schedule" the right module for this task?

CodePudding user response:

until function does not take a day as input instead it takes a timedelta. You can learn more from here https://schedule.readthedocs.io/en/stable/examples.html

import schedule
from datetime import datetime, timedelta, time

def job():
    print('Boo')

# run job until a 18:30 today
schedule.every(1).hours.until("18:30").do(job)

# run job until a 2030-01-01 18:33 today
schedule.every(1).hours.until("2030-01-01 18:33").do(job)

# Schedule a job to run for the next 8 hours
schedule.every(1).hours.until(timedelta(hours=8)).do(job)

# Run my_job until today 11:33:42
schedule.every(1).hours.until(time(11, 33, 42)).do(job)

# run job until a specific datetime
schedule.every(1).hours.until(datetime(2020, 5, 17, 11, 36, 20)).do(job)

If we calculate sunday 11pm to friday 11pm it will be total 120 hours. So, timedelta(hours=120). Then your code should be like as below

import time
from datetime import datetime, timedelta

def script_sample():
  now = datetime.now()
  print(now)

schedule.every(5).minutes.sunday.at("23:00").until(timedelta(hours=120)).do(script_sample)
while True:
  schedule.run_pending()

hope this helps.

CodePudding user response:

You cannot directly set until condition as the day of week and time in schedule API but can set an explicit datetime, time, or a timedelta object as an argument in the until() function. See apidocs.

Also, you cannot schedule a job with a day of week and every x minutes. Something like schedule.every(5).minutes.sunday.at("23:00") is not allowed.

Try something like this to first find the date of next Sunday then calculate the following Friday date from it. Now you have the starting and ending times.

Next, you can call sleep until the start time, schedule the job to run every 5 minutes until Friday at 23:00.

import time
from datetime import datetime, timedelta
import schedule

def script_sample():
    now = datetime.now()
    print(now)

now = datetime.now()
# find date of next sunday
d = now
# weekday():  Monday is 0 and Sunday is 6
while d.weekday() != 6:
    d  = timedelta(days=1)
d = d.replace(hour=23, minute=00, second=0, microsecond=0)
print("now=", now)
print("next sunday", d) # start date
wait_time = d - now

wait_time_secs = wait_time.total_seconds()
if wait_time_secs > 0:
    print("wait time is ", wait_time)
    print("waiting to start...")
    time.sleep(wait_time_secs)
else:
    print("no need to wait. let's get started')

# This code will be reached here when it is Sunday at 23:00 or just before midnight on Sunday.

# next find date of next Friday (friday=5)
while d.weekday() != 5:
    d  = timedelta(days=1)
endtime = d.replace(hour=23, minute=00, second=0, microsecond=0)
print("end time", endtime)

# now schedule a job every 5 mins until the end time
schedule.every(5).minutes.until(endtime).do(script_sample)

while True:
    schedule.run_pending()
  • Related