3

I have encountered a 1 second delay in signal delivery, when I send a signal to a thread in python.

Sending thread snippet (import both signal and os (and time)):

print "Signal sent at:", time.time()
os.kill(other_thread_id, signal.SIGALRM)

Receiving thread snippet:

def receive_alarm(signum, stack):
    print "Signal received at:", time.time()
signal.signal(signal.SIGALRM, receive_alarm)

Running both simultaneously gives:

Signal sent at 1423141616.02
Signal received at: 1423141617.02

So delivery of the alarm signal takes a full second. It's always exactly 1 second. This delay makes it unusable for my purpose. Does anyone know more about this? Is it a setting somewhere? Or am I doing something else wrong? I've tried other signals as well.

OS: Debian 7

UPDATE after comments below:

strace python main.py

DOESN'T give this delay.

python main.py

DOES :-). This might get us closer to what's wrong.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • I would believe it is Python specific. With plain C code, you won't have a one second delay. – Basile Starynkevitch Feb 05 '15 at 13:18
  • Ah that's useful information. It seems weird that either the sender (os.kill) or the receiver (signal.signal) has a built-in 1s delay though. I'll investigate a.s.a.p. (might take up to 48hrs or so, I'm pretty busy). Meanwhile -other ideas/ fixes/ confirmations that this is python specific -are highly appreciated. – Johan Rensink Feb 05 '15 at 13:24
  • Blind guess: Python would use pipes and `poll` them for signal delivery. Try to use `strace` to find out. – Basile Starynkevitch Feb 05 '15 at 13:25
  • When I strace it, the delay disappears (which is kinda good news!)! So it's some kind of environment setting perhaps? – Johan Rensink Feb 05 '15 at 13:39
  • Still looking: 'strace -ttT python main.py' gives very little delay (less then 50ms). 'python main.py' gives the full second delay still. Is there somthing similar to sys.stdout.flush() (i.e. flushing the output buffer to the screen) that works with signals perhaps? – Johan Rensink Feb 05 '15 at 16:40
  • I found a workaroun: using unix socket (I didn't know they existed 24 hours ago. [Here is a good, simple tutorial](http://www.velvetcache.org/2010/06/14/python-unix-sockets)). The delivery is way faster then the signals (Basile was probably right that it's a polling issue, and strace just does faster polling). – Johan Rensink Feb 06 '15 at 17:42
  • Nice you got it working with unix sockets ;) – 0x4a50 Feb 06 '15 at 17:49
  • You might consider using the Linux specific [signalfd(2)](http://man7.org/linux/man-pages/man2/signalfd.2.html) – Basile Starynkevitch Feb 06 '15 at 17:52
  • Can we see the rest of the code? – Davis Herring Feb 25 '22 at 05:53

0 Answers0