Search code examples
pythonmultithreadingtimeoutpython-multithreading

How to limit the execution time of function calls in loop


I have a loop which calls a function which sometimes hangs (example below). I found a couple examples of how we might limit the execution time of a function call. However, they fail in loop situations as we might be midway through the next loop when the interrupt signal occurs. Is there any way to limit the execution time for the below code?

def perhaps_long_exec():
  if random.random() < 0.5:
    while True:
      pass

for i in range(100)
  # limit execution time of below function call
  perhaps_long_exec()

We assume perhaps_long_exec() is an external function that cannot be modified. I have just given an example function as to what may be inside the function, but the function itself is a black box.


Solution

  • Using multiprocessing seems like a decent solution. Make a new process for every loop and kill it if it's stuck.

    import random
    import time
    from multiprocessing import Process
    
    
    def perhaps_long_exec():
        x = random.random()
        print(x)
        if x < 0.5:
            print('Stuck!')
            while True:
                pass
        else:
            print('Fine')
    
    
    if __name__ == '__main__':
        for i in range(100):
            p = Process(target=perhaps_long_exec)  # make process
            p.start()  # start function
            time.sleep(3)  # wait 3 secs
            if p.is_alive(): # check if process is still going
                p.terminate()  # kill it
                print('Killed')
    

    Output:

    > 0.26936380878403265
    > Stuck!
    > Killed
    > 0.5185183414607246
    > Fine
    > 0.4361287927947891
    > Stuck!
    > Killed