-1

Let's say that I have the following code:

possibilities = range(20)
input_name = 4
for i in possibilities:
  exec("import Function_" + str(i))
  exec("Function_" + str(i) + ".solveproblem(" + str(input_name) + ")")

Since the second exec function, e.g. Function_3.solveproblem(4), can take infinity amount of time, I would just like to try (the function also can have some error) at most for 1,000 seconds and if the time exceeds, then I would like to stop that execution and go through the next function from another python file.

Any idea?

Ch3steR
  • 20,090
  • 4
  • 28
  • 58

1 Answers1

0

You could try a timer with time

base = time.time() # Get time before start
for each in possibilities:
    ''do your stuff''
    if time.time()-base>100:
        break # Time limit

Or if it is impossible to slide it in, consider starting another thread

def timer:
    base = time.time() # Get time before start
    while True:
        if time.time()-base==100:
            thread.interrupt_main()

            break # Time limit
thread = threading.Thread(target=timer)

thread.start()

'''
now do the rest
'''

How to exit the entire application from a Python thread?

Cheng An Wong
  • 308
  • 2
  • 8