Looking for a way to pause (and then later resume) a Python script every x minutes (with a small random +/-). The original script would run over and over and then every x minutes it would pause for a set amount (again with a random +/-) then continue.
Asked
Active
Viewed 367 times
2 Answers
0
import time
import random
# this will pause between 1 & 10 minutes, randomly.
time.sleep(60*(random.randint(1, 10)))

Dharman
- 30,962
- 25
- 85
- 135

Miguel Agurto
- 51
- 3
-
-
This will pause for 1 - 10 min. (Random). If you have need an specific time you can change to this: time.sleep(60*5) #this will pause for 5 minutes. – Miguel Agurto Jun 24 '21 at 00:26
0
Here's an example code using random.uniform()
to generate a random float and time.sleep()
to wait for a given time.
import random
import time
BASE_DELAY = 60 # base amount in seconds
RAND_MAX = 30 # high end of random in seconds
RAND_MIN = -30 # low end of random in seconds
running = True
def do_stuff():
#do stuff here, maybe setting running to False
pass
def loop():
while running:
time.sleep(BASE_DELAY + random.uniform(RAND_MIN, RAND_MAX))
do_stuff()
loop()

Antricks
- 171
- 1
- 9
-
-
You can either replace the call to `do_stuff()` with the things you want to do, or you can add them instead of `pass` inside the `do_stuff` function. (By the way, I like to help, but please try to find solutions to your problems yourself first. Maybe watch a Python Tutorial that covers the basics - there are lots of really good resources out there. The concepts I used here are loops and functions, so it's relatively simple stuff) – Antricks Jun 24 '21 at 04:52