-1

I am running a Python script on a Windows machine that needs to exit from the while loop when timeout is reached or flag is True:

import time
start_time = time.time()
flag = False
timeout = 5

while time.time() < timeout + start_time:
    # DO something
    flag = True
    break

if flag is False:
   print(f"Timeout reached {timeout}")

With the current code, the timeout or the flag are not hit. Any hints of what it is wrong?

Albert
  • 191
  • 1
  • 3
  • 23
  • But since you set the flag to true, and break, it'd just do that "something" once. – ewokx Sep 26 '22 at 09:10
  • I would need that something to run in a certain time. If exceeds it, print that message. – Albert Sep 26 '22 at 09:23
  • 1
    The while-condition is checked once per iteration. It is not continually monitored while the block of code is executed. – khelwood Sep 26 '22 at 09:33

3 Answers3

1

Try this solution without a loop, as shown here: https://stackoverflow.com/a/14924210/7988650

If you want to use a python package, you could use this one https://pypi.org/project/wrapt-timeout-decorator/ to make things shorter by just annotating your function with an @timeout()

import multiprocessing

def do_something():
    # Do something

if __name__ == '__main__':
    timeout = 5
    # Start do_something as a process
    p = multiprocessing.Process(target=do_something)
    p.start()
    # Wait for 5 seconds or until process finishes
    p.join(timeout)
    # If thread is still active
    if p.is_alive():
        print(f"Timeout reached {timeout}")
        # Terminate - may not work if process is stuck for good
        p.terminate()
        # OR Kill - will work for sure, no chance for process to finish nicely however
        # p.kill()
        p.join()

  • `if flag` in this case will execute when flag is true and I would like to print the message when flag is false. – Albert Sep 26 '22 at 09:23
  • The logic is wrong then because you are setting the flag to True when the timeout is reached i.e., the end of the while loop. – Kyriakos Psarakis Sep 26 '22 at 09:24
  • Thanks for your reply. The logic is correct because if `flag` sets to True I know that the `DO something` went successfully through the end and is executed before the timeout is reached. Nothing wrong with the logic. – Albert Sep 26 '22 at 09:26
  • It is the wrong way to do it, you cannot measure this the way you do it. Now that I understood your question, I will try to come up with a solution. – Kyriakos Psarakis Sep 26 '22 at 09:28
  • I know that this solution looks a bit complex, but there is no way to timeout a function (while loop) within that same function. – Kyriakos Psarakis Sep 26 '22 at 09:44
  • Thanks for solution. It wasn't working for me as I was using some Python library in DO SOMETHING that was in conflict with multithreading. I have added anyway for your answer +1 as this solution was working for others. – Albert Oct 03 '22 at 10:43
0

I am going to post the solution that I have found and worked for me as in the #DO SOMETHING section I had some blocks of code that were using some libraries that were in conflict with multithreading public library:

import time

MAX_TIMEOUT = 5

start_time = time.time()
while True:
    time_delta = time.time() - start_time
    while time_delta <= MAX_TIMEOUT:
        # DO SOMETHING
        finished = True
        break
        if finished:
           break
        if time_delta > MAX_TIMEOUT:
            print("Timeout reached.")
            break
Albert
  • 191
  • 1
  • 3
  • 23
  • If `do something` is a blocking operation, the `do something` won't be stoped once the timeout is reached, making the timeout, not a timeout but just a print statement. If the `multiprocessing` solution didn't work for you, I think giving a timeout decorator package a try would be better. – Kyriakos Psarakis Oct 03 '22 at 11:55
-1

It looks like you are adding up the start_time to timeout. Which makes it practically infinite.

try while time.time() < timeout:

  • Thanks for your reply. time.time() starts from the last epoch which is from 1st of January 1970 and is a big number. – Albert Sep 26 '22 at 09:30