interruptingcow is a generic Python utility that can relatively gracefully interrupt your code when it doesn't execute within a specific number of seconds:
from interruptingcow import timeout
# perform a potentially very slow operation
print "didn't finish within 5 seconds"
pip install interruptingcow
Interruptingcow uses signal(SIGALRM) to let the operating system interrupt program execution. This has the following limitations:
1. Python signal handlers only apply to the main thread, so you cannot use this from other threads
2. You must not use this in a program that uses SIGALRM itself