title: Python signal woes: SIGQUIT handler delays execution if SIGQUIT received during execution of another signal handler?
body: <p>The following program is very simple: it outputs a single dot each half a second. If it recieves a <strong>SIGQUIT</strong>, it proceeds to output ten <strong>Q</strong>s. If it recieves a <strong>SIGTSTP</strong> <em>(<kbd>Ctrl</kbd>-<kbd>Z</kbd>)</em>, it outputs ten <strong>Z</strong>s.</p>
  <p>If it recieves a <strong>SIGTSTP</strong> while printing <strong>Q</strong>s, it will print ten <strong>Z</strong>s after it's done with the ten <strong>Q</strong>s. This is a good thing.</p>
  <p>However, if it recieves a <strong>SIGQUIT</strong> while printing <strong>Z</strong>s, it fails to print <strong>Q</strong>s after them. Instead, it prints them out only after I manually terminate execution via a KeyboardInterrupt. I want the <strong>Q</strong>s to be printed immediately after the <strong>Z</strong>s.</p>
  <p>This happens using Python2.3.</p>
  <p>What am I doing wrong?</p>
  <pre><code>#!/usr/bin/python
  
  from signal import *
  from time import sleep
  from sys import stdout
  
  def write(text):
      stdout.write(text)
      stdout.flush()
  
  def process_quit(signum, frame):
      for i in range(10):
          write(&quot;Q&quot;)
          sleep(0.5)
  
  def process_tstp(signum, frame):
      for i in range(10):
          write(&quot;Z&quot;)
          sleep(0.5)
  
  signal(SIGQUIT, process_quit)
  signal(SIGTSTP, process_tstp)
  
  while 1:
      write('.')
      sleep(0.5)
  </code></pre>