Tech and travel

Using signal.alarm() for a timeout

2014-01-16

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.

Copyright (c) 2024 Michel Hollands