schedule a job with a random interval

210 views
Skip to first unread message

Giulio Coa

unread,
Apr 29, 2020, 10:18:23 AM4/29/20
to APScheduler
I have this part of code:
    scheduler.add_job(func, IntervalTrigger(minutes=random.randint(1, 10), timezone="Europe/Rome"))
The job will be rescheduled every time with a different interval ?

Alex Grönholm

unread,
May 8, 2020, 3:43:24 PM5/8/20
to Giulio Coa, apsch...@googlegroups.com

The point I was trying to make is that what you're doing here is calling random.randint(1, 10) once and then constructing an IntervalTrigger using the result.
You seem to be under the impression that your code passes a function that generates random integers between 1 and 10 and calls that function every time it needs to calculate the next run time. That is not how this works. Even if you did pass such a function to IntervalTrigger (which you didn't), it would not work because IntervalTrigger assumes that "minutes" is a simple numeric value, and not a function.

To do what you wanted to achieve, you could create a custom trigger class that creates a timedelta with a randomized "minutes" value and uses that to construct the next fire time.

It might also be possible to use a plain IntervalTrigger with a sufficiently large "jitter" value to achieve the same effect.

On 8.5.2020 20.29, Giulio Coa wrote:
What do you mean ?
I want that the job will be rescheduled every time at a random value between 1 and 10.
So, if I have this code it will be done ?
scheduler.add_job(func, IntervalTrigger(minutes=random.randint(1, 10), timezone="Europe/Rome"))

Il giorno mar 5 mag 2020 alle ore 09:43 Alex Grönholm <alex.g...@nextday.fi> ha scritto:

No. How do you suppose this is any different from:

x = random.randint(1, 10)
scheduler.add_job(func, IntervalTrigger(minutes=x, timezone="Europe/Rome"))

--
You received this message because you are subscribed to the Google Groups "APScheduler" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apscheduler...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/apscheduler/df4731d3-ddb0-4710-b429-c173d3df46a9%40googlegroups.com.


--
Giulio Coa.

Giulio Coa

unread,
May 14, 2020, 4:49:58 AM5/14/20
to apsch...@googlegroups.com
I have writed this class for do what I said, but I don't known how can re-extract a random number at every re-scheduling of the job.
Can you help me ?

triggers.py

Alex Grönholm

unread,
May 14, 2020, 1:43:21 PM5/14/20
to apsch...@googlegroups.com

Maybe you should start by showing what you have.

Giulio Coa

unread,
May 14, 2020, 1:59:08 PM5/14/20
to apsch...@googlegroups.com
This is what I have



--
Giulio Coa.
main.py
triggers.py

Alex Grönholm

unread,
May 14, 2020, 2:05:42 PM5/14/20
to apsch...@googlegroups.com

You need to override the get_next_fire_time() method. Generate the random value there and use the timedelta class to add that to the previous fire time. Look at the existing code for clues on how it works.

Giulio Coa

unread,
May 14, 2020, 2:31:10 PM5/14/20
to apsch...@googlegroups.com
Like this ?



--
Giulio Coa.
triggers.py

Giulio Coa

unread,
May 14, 2020, 2:33:17 PM5/14/20
to apsch...@googlegroups.com
I have fixed a bug in the code.
Sorry 😅
--
Giulio Coa.
triggers.py

Giulio Coa

unread,
May 28, 2020, 4:57:29 AM5/28/20
to apsch...@googlegroups.com
Hi
There are news ?
--
Giulio Coa.

Alex Grönholm

unread,
May 28, 2020, 5:19:32 AM5/28/20
to apsch...@googlegroups.com

Sorry, I thought you didn't need more help.

Your code still needs to handle the case where previous_fire_time is None. I suggest that you look at the original code for examples on how to handle that.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages