How to implement multiple signals in a program? <p>I am starting to learn Python (newbie), so not much idea about the different modules etc.</p> <p>Scenario that I want to simulate:</p> <p>I have a program <code>prg1.py</code> that I want to run for some user defined time say <code>t</code> seconds. After this time (<code>t</code> seconds), the program should exit. For this I am using <code>signal.signal()</code> to create alarm. Below is the working code:</p> <pre> import signal import time import sys def receive_alarm(signum, stack): sys.exit('Exiting!') signal.signal(signal.SIGALRM, receive_alarm) signal.alarm(10) while 1: print 'Working...' time.sleep(1) </pre> <p>The program runs for 10 seconds and then exits as expected.<br> Note: The while loop below is just for testing, it would be replaced by my working code.</p> <p>Now I want to implement multiple signals to do different tasks at different intervals of time. </p> <p>e.g. In <code>EVERY</code>: <br> <code>5</code> seconds: execute a specific function <code>fun1()</code> <br> <code>10</code> seconds: execute a specific function <code>fun2()</code>, and so on... (tasks I want to perform in the program)</p> <p>I tried adding another alarm as shown below, but didn't work:</p> <pre><code>import signal import time import sys def receive_alarm(signum, stack): sys.exit('Exiting!') def receive_alarm_two(signup, stack): print 'Call to other functions!' signal.signal(signal.SIGALRM, receive_alarm) signal.alarm(10) # Second Alarm signal.signal(signal.SIGALRM, receive_alarm_two) signal.alarm(2) while 1: print 'Working...' time.sleep(1) </code></pre> <p>This doesn't work! Simple exits without any error or exit message :(</p> <p>How can this functionality be implemented?</p> <p>NOTE: <code>Use of Threads is restricted.</code><br></p> <p>NOTE: <code>As I want the program to keep listening to different signals, it can't sleep i.e. cannot use time.sleep().</code></p>