Python, mod_wsgi and persistance

Asked by Dr Haver on 2010-03-09

I am doing a simple "Apache / mod_wsgi / python" app (no Framework needed) that will deliver PDFs from a path specified in a MySQL DB.

The below code is using a hard-coded PDF for testing. I want to maintain a pool to MySQL.

However, everytime the code below is called from Apache, won't ALL the objects be destroyed ? - including your PySQLPoolConnection obect?

How will the PySQLPoolConnetion objects survive the next connection from Apache? Are you launching a separate thread in the background that keeps going ?? How do we close the thread if Apache is no longer running ??

def application(environ, start_response):
    status = '200 OK'


           # I want to put connetion = PySQLConnection(...........) right here
           # i want put query = PySQLQuery (..............)
           # filename = QueryResut[myPath]

    fileptr = open(filename,"rb") # read wholefile in (ad binary)
    output =

    response_headers = [('Content-type', 'application/pdf'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

Question information

English Edit question
PySQLPool Edit question
No assignee Edit question
Last query:
Last reply:
Nick Verbeck (nerdynick) said : #1

So the way apache/mod_wsgi works is to keep a running copy of your application per process for Prefork mode or per thread pool in Worker mode. So as your application never really dies between requests. PySQLPool will continue to keep your connections to MySQL in its pool tell Apache chooses to terminate that process.

Just make sure to configure your PySQLPool settings to the appropriate max_connections and connection_timeout interval. As this will help to solve some orphaned socket problems with connections to you DB .

This answer also explains a little more indepth of some problems you may face with Apache & PySQLPool

Can you help with this problem?

Provide an answer of your own, or ask Dr Haver for more information if necessary.

To post a message you must log in.