views:

105

answers:

2

Well, at least a mystery to 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)

Works as expected i.e. delivers a "beat!" message every 2 seconds. Next, no output is produced:

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 issue?

+4  A: 

From my system's man setitimer (emphasis mine):

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

ITIMER_REAL decrements in real time, and delivers SIGALRM upon expiration.

ITIMER_VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expiration.

Did you just miss that your process isn't executing while sleeping? It's going to take an awfully long time for you to accrue actually-used time with that loop.

Roger Pate
It looks like a good explanation to me. When you do `time.sleep()`, your process is suspended (i.e. not executing). If you changed it to `pass`, your process would execute and consume time.
Gabe
How silly of me!!! The above was just a quick test to see how timer signals work... newbie (on signals) error. Thanks!!
jldupont
+3  A: 

The signal.ITIMER_VIRTUAL only counts down with the process is running. time.sleep(5) suspends the process so the timer doesn't decrement.

Stephen L
+1: thanks..........
jldupont