0

I have a Python algorithm that will not terminate on its own. I'm doing some profiling on it and would like to run it with a bunch of different settings for a set amount of time, terminating it after that time has elapsed and starting the next run. How do I do this from within Python?

for c in configs:
  # what do I wrap this with do terminate it after a 
  # set amount of time and go to next loop iteration?
  runalgorithm(config)  
Sean Mackesey
  • 10,701
  • 11
  • 40
  • 66

1 Answers1

0
start = time.time()
endt = start + 30
while True:
    now = time.time()
    if now > endt:
        return
    else:
        print end - start

Try something like this within your for loop!

Kurt
  • 196
  • 1
  • 14
  • Thanks, but this won't work because `runalgorithm` runs forever; therefore the time checking code will never get called. Of course I could alter the algorithm with something like that, but I shouldn't have to change the algorithm. – Sean Mackesey Jun 24 '14 at 21:01