Skip to content

Using signal.alarm() for a timeout

The Python signal module let’s you setup Unix signals, including SIGALRM. This can be used to setup a timeout :


import signal

def handler(signum, frame):
print 'received', signum

# Setup the handler
signal.signal(signal.SIGALRM, handler)

# Trigger the alarm after 60 seconds
signal.alarm(60)

This prints ‘received 14’ after 60 seconds.