As in this question, I'd also like to run some code for a limited amount of time. However, I'd like to recover the value of some variables when the code is interrupted, which the answers in the question do not allow. A dummy example would be:
import time
limit = 10
counter = 0
start = time.time()
while(time.time() - start < limit):
counter += 1
and this way I can recover counter
after running the code for limit
seconds. But I do not want to use this approach, because if the operation in the while loop takes a long time (e.g. > limit
), it is still executed once - which I don't want.
How could I avoid this, but still get the value of some variables when the process is interrupted?