1๐
โ
(take 2)
This is the function Iโm using, not written by me, but I forget where I found it. Even if your scheduler is being called multiple times, it will at least remove any existing jobs.
def schedule_once(scheduled_time, func, args=None, kwargs=None,
interval=None, repeat=None, result_ttl=None, timeout=None, queue_name=None):
"""
Schedule job once or reschedule when interval changes
"""
if not func in functions or not interval in functions[func] \
or len(functions[func]) > 1:
# clear all scheduled jobs for this function
map(scheduler.cancel, filter(lambda x: x.func == func, jobs))
# schedule with new interval
scheduler.schedule(scheduled_time, func, interval=interval, repeat=repeat)
schedule_once(
scheduled_time=datetime.utcnow(), # Time for first execution, in UTC timezone
func=readmail, # Function to be queued
interval=120, # Time before the function is called again, in seconds
repeat=0 # Repeat this number of times (None means repeat forever)
)
๐คPhoebeB
Source:stackexchange.com