title: How do you control MySQL timeouts from SQLAlchemy?
body: <p>What's the right way to control timeouts, from the client, when running against a MySQL database, using SQLAlchemy?  The <code>connect_timeout</code> URL parameter seems to be insufficient.</p>
  
  <p>I'm more interested in what happens when the machine that the database is running on, e.g., disappears from the network unexpectedly.  I'm not worried about the queries themselves taking too long.  </p>
  
  <p>The following script does what you'd expect (i.e., time out after approximately one second) if <em>somehost</em> is unavailable <strong>before</strong> the <code>while</code> loop is ever reached.  But if <em>somehost</em> goes down <strong>during</strong> the <code>while</code> loop (e.g., try yanking out its network cable after the loop has started), then the timeout seems to take at least 18 seconds.  Is there some additional setting or parameter I'm missing?</p>
  
  <p>It's not surprising that the <code>wait_timeout</code> session variable doesn't work, as I think that's a server-side variable.  But I threw it in there just to make sure.</p>
  
  <pre><code>from sqlalchemy import *
  from sqlalchemy.exc import *
  import time
  import sys
  
  engine = create_engine("mysql://user:password@somehost/test?connect_timeout=1")
  try:
      engine.execute("set session wait_timeout = 1;")
      while True:
          t = time.time()
          print t
          engine.execute("show tables;")
  except DBAPIError:
      pass
  finally:
      print time.time() - t, "seconds to time out"
  </code></pre>