signal.alarm not triggering exception on time
<p>I've slightly modified the signal example from the <a href="https://docs.python.org/2/library/signal.html#signal.alarm" rel="noreferrer">official docs</a> (bottom of page).</p>

<p>I'm calling <code>sleep 10</code> but I would like an alarm to be raised after 1 second. When I run the following snippet it takes way more than 1 second to trigger the exception (I think it runs the full 10 seconds).</p>

<pre><code>import signal, os

def handler(signum, frame):
    print 'Interrupted', signum
    raise IOError("Should after 1 second")

signal.signal(signal.SIGALRM, handler)
signal.alarm(1)

os.system('sleep 10')

signal.alarm(0)
</code></pre>

<p>How can I be sure to terminate a function after a timeout in a single-threaded application?</p>