timeout of a function in python - Serbipunk/notes GitHub Wiki

https://stackoverflow.com/questions/1191374/using-module-subprocess-with-timeout

threading.Thread.join(time)

https://stackoverflow.com/questions/492519/timeout-on-a-function-call

https://stackoverflow.com/a/14924210

import multiprocessing
import time

# bar
def bar():
    for i in range(100):
        print "Tick"
        time.sleep(1)

if __name__ == '__main__':
    # Start bar as a process
    p = multiprocessing.Process(target=bar)
    p.start()

    # Wait for 10 seconds or until process finishes
    p.join(10)

    # If thread is still active
    if p.is_alive():
        print "running... let's kill it..."

        # 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()