I am trying to schedule a job to run every 3 minutes on average, with a component of randomness between +/- 30 seconds. So the next job runs anywhere between 2mins 30secs - 3mins 30secs later.
This code works nicely for exactly 3 minutes, but I can't think of a way to introduce the 30 secs of randomness:
import schedule
def job:
print('hi')
schedule.every(3).minutes.do(job)
You need to look up the random
module that comes with python.
import schedule
import random
def job():
print('hi')
two_mins_30 = 2 * 60 + 30
schedule.every(two_mins_30 + random.randint(0, 60)).seconds.do(job)
This calculation is: two minutes, 30 plus up to another minute at random.
Update:
It turns out you can directly do this with schedule
because the Job
class has a to()
method:
schedule.every(two_mins_30).to(two_mins_30 + 60).seconds.do(job)