title: python MySQLDB query timeout
body: <p>I'm trying to enforce a time limit on queries in python MySQLDB. I have a situation where I have no control over the queries, but need to ensure that they do not run over a set time limit. I've tried using signal.SIGALRM to interrupt the call to execute, but this does not seem to work. The signal gets sent, but does not get caught until after the call to execute finishes.</p>
  
  <p>I wrote a test case to prove this behavior:</p>
  
  <pre><code>#!/usr/local/bin/python2.6
  
  import time
  import signal
  
  from somewhere import get_dbc
  
  class Timeout(Exception):
      """ Time Exceded """
  
  def _alarm_handler(*args):
      raise Timeout
  
  dbc = get_dbc()
  
  signal.signal(signal.SIGALRM, _alarm_handler)
  signal.alarm(1)
  
  try:
      print "START:  ", time.time()
      dbc.execute("SELECT SLEEP(10)")
  except Timeout:
      print "TIMEOUT!", time.time()'
  </code></pre>
  
  <p>The "SELECT SLEEP(10)" is simulating a slow query, but I do see the same behavior with an actual slow query.</p>
  
  <p>The Result:</p>
  
  <pre><code>START:   1254440686.69
  TIMEOUT! 1254440696.69
  </code></pre>
  
  <p>As you can see, it's sleeping for 10 seconds then I get the Timeout Exception. </p>
  
  <p>Questions: </p>
  
  <ol>
  <li>Why do I not get the signal until after execute finishes?</li>
  <li>Is there another reliable way to limit query execution time?</li>
  </ol>