Stop code after time period

Python

Python Problem Overview


I would like to call foo(n) but stop it if it runs for more than 10 seconds. What's a good way to do this?

I can see that I could in theory modify foo itself to periodically check how long it has been running for but I would prefer not to do that.

Python Solutions


Solution 1 - Python

Here you go:

import multiprocessing
import time

# Your foo function
def foo(n):
    for i in range(10000 * n):
        print "Tick"
        time.sleep(1)

if __name__ == '__main__':
    # Start foo as a process
    p = multiprocessing.Process(target=foo, name="Foo", args=(10,))
    p.start()

    # Wait 10 seconds for foo
    time.sleep(10)

    # Terminate foo
    p.terminate()

    # Cleanup
    p.join()

This will wait 10 seconds for foo and then kill it.

Update

Terminate the process only if it is running.

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

    # Terminate foo
    p.terminate()

Update 2 : Recommended

Use join with timeout. If foo finishes before timeout, then main can continue.

# Wait a maximum of 10 seconds for foo
# Usage: join([timeout in seconds])
p.join(10)

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

    # Terminate foo
    p.terminate()
    p.join()

Solution 2 - Python

import signal

#Sets an handler function, you can comment it if you don't need it.
signal.signal(signal.SIGALRM,handler_function) 

#Sets an alarm in 10 seconds
#If uncaught will terminate your process.
signal.alarm(10) 

The timeout is not very precise, but can do if you don't need extreme precision.

Another way is to use the resource module, and set the maximum CPU time.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongraffeView Question on Stackoverflow
Solution 1 - PythonATOzTOAView Answer on Stackoverflow
Solution 2 - PythonLtWorfView Answer on Stackoverflow