Python timer mystery

Well, at least a riddle for me. Consider the following:

import time import signal def catcher(signum, _): print "beat!" signal.signal(signal.SIGALRM, catcher) signal.setitimer(signal.ITIMER_REAL, 2, 2) while True: time.sleep(5) 

It works as expected, that is, it delivers a "bit!". message every 2 seconds. Then the conclusion is not made:

 import time import signal def catcher(signum, _): print "beat!" signal.signal(signal.SIGVTALRM, catcher) signal.setitimer(signal.ITIMER_VIRTUAL, 2, 2) while True: time.sleep(5) 

Where is the problem?

+8
python signals timer
Feb 18 '10 at 20:43
source share
2 answers

From my system man setitimer (my selection):

The system provides each process with three timers with an interval, each of which decreases in a separate time domain . When any timer expires, the signal is sent to the process, and the timer (potentially) restarts.

ITIMER_REAL decreases in real time and delivers SIGALRM upon expiration.

ITIMER_VIRTUAL is reduced only when the process is running, and upon expiration, it delivers SIGVTALRM.

Did you just miss that your process is not running during sleep? This will take a very long time so that you can actually use the time with this loop.

+14
Feb 18 '10 at 20:48
source share
β€” -

signal.ITIMER_VIRTUAL only counts when the process is running. time.sleep(5) pauses the process, so the timer does not decrease.

+3
Feb 18 '10 at 20:49
source share



All Articles