title: set transaction\query timeout in psycopg2?
body: <p>Is there a way to set a timeout in <a href="https://pypi.python.org/pypi/psycopg2" rel="noreferrer">psycopg2</a> for db transactions or for db queries?</p>
  
  <p><strong>A sample use-case:</strong><br>
  Heroku limits django web requests to 30sec, after which Heroku terminates the request without allowing django to gracefully roll-back any transactions which have not yet returned. This can leave outstanding transactions open on postgres. You could configure a timeout in the database, but that would also limit non-web-related queries such as maintenance scripts analytics etc. In this case setting a timeout via the middleware (<a href="https://stackoverflow.com/q/19963854/348545">or via django</a>) would be preferable.</p>