If I have a infinite while
loop, how can I have the loop run the next iteration every 10 minutes from the start of the loop iteration?
If the first iteration starts at 1:00am and finishes at 1:09am, the next iteration should run at 1:10am instead of waiting for another 10 minutes (like in the code snippet below). If the loop iteration took more than 10 minutes to run, the next iteration should run immediately and start the countdown of the next 10 minutes.
while(True):
someLongProcess() # takes 5-15 minutes
time.sleep(10*60)
Example
Loop 1: Starts 1:00am, ends 1:09am
Loop 2: Start 1:10am, ends 1:25am # ends 5 minutes later
Loop 3: Starts 1:25am, ends 1:30am # ends 5 minutes earlier
Loop 4: Starts 1:35am, ends 1:45am
You can keep a counter of guesses , e.g. guesses = 0 . Then, at the end of your while_loop, guesses += 1 . Your condition can be while guesses < 3 for example, to limit it to 3 guesses. And then, instead of keeping track of found , just break out when user_guess == random_number .
Infinite While Loop in Python a = 1 while a==1: b = input(“what's your name?”) print(“Hi”, b, “, Welcome to Intellipaat!”) If we run the above code block, it will execute an infinite loop that will ask for our names again and again. The loop won't break until we press 'Ctrl+C'.
If the Python program outputs data, but you never see that output, that's a good indicator you have an infinite loop. You can test all your functions in the repl, and the function that does "not come back" [to the command prompt] is a likely suspect.
An Infinite Loop in Python is a continuous repetitive conditional loop that gets executed until an external factor interferes in the execution flow, like insufficient CPU memory, a failed feature/ error code that stopped the execution, or a new feature in the other legacy systems that needs code integration.
Remember the start time, calculate sleep time using that.
while True:
start = time.time()
some_long_process()
end = time.time()
remain = start + 10*60 - end
if remain > 0:
time.sleep(remain)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With