0

I want to run a file I have once every hour but need to avoid patterns. Example of what I need if I ran it at 1 PM:

1:12 PM
2:26 PM
3:24 PM
4:59 PM
5:02 PM

Currently, I have this:

import schedule
import time
import random

def randomS():
    min_wait = random.randint(0,59)
    time.sleep(min_wait*60)
    run()
def run():
    # exec(open('Mudae.py').read())
    seconds = time.time()
    local_time = time.ctime(seconds)
    print(local_time)

schedule.every(1).hours.do(randomS)

randomS()

while True:
    schedule.run_pending()

The problem with this code is that this code will occasionally skip an entire hour.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 2
    why not set the next absolute time to (hour+1: random.randint(0,59)) then find out how many minutes that is? – RufusVS Jun 07 '21 at 19:18
  • There is no module named `schedule` in the standard library, so what are you using? – martineau Jun 07 '21 at 20:30
  • https://schedule.readthedocs.io/en/stable/ – Alioth Jun 07 '21 at 21:27
  • Since it's a third-party module I'm not going to install it just to answer your question. That said, because of the way your code chooses the amount of time to wait, it seems fairly obvious why it might skipover an hour once in while — and that you could simply check for it and pick another random amount of time to sleep when it happens. – martineau Jun 07 '21 at 21:48
  • Please don't put the solution/answer in the question. Stack Overflow has a Q&A format, so answers should go in the Answers section. You already self-answered it but deleted: https://stackoverflow.com/a/67880359/2745495. [It is OK (even encouraged) to answer your own questions](https://stackoverflow.com/help/self-answer). I suggest to undelete that, put some short explanation how it solves your problem, and mark it as accepted if it indeed solved your problem. – Gino Mempin Jun 08 '21 at 01:34

0 Answers0