Domanda

I'm writing a web application that connects to a database. I'm currently using a variable in a module that I import from other modules, but this feels nasty.

# server.py
from hexapoda.application import application

if __name__ == '__main__':
  from paste import httpserver
  httpserver.serve(application, host='127.0.0.1', port='1337')

# hexapoda/application.py
from mongoalchemy.session import Session
db = Session.connect('hexapoda')

import hexapoda.tickets.controllers

# hexapoda/tickets/controllers.py
from hexapoda.application import db

def index(request, params):
  tickets = db.query(Ticket)

The problem is that I get multiple connections to the database (I guess that because I import application.py in two different modules, the Session.connect() function gets executed twice).

How can I access db from multiple modules without creating multiple connections (i.e. only call Session.connect() once in the entire application)?

È stato utile?

Soluzione

That's probably not what you want to do - a single connection per app means that your app can't scale.

The usual solution is to connect to the database when a request comes in and store that connection in a variable with "request" scope (i.e. it lives as long as the request).

A simple way to achieve that is to put it in the request:

request.db = ...connect...

Your web framework probably offers a way to annotate methods or something like a filter which sees all requests. Put the code to open/close the connection there.

If opening connections is expensive, use connection pooling.

Altri suggerimenti

Try the Twisted framework with something like:

from twisted.enterprise import adbapi

class db(object):

    def __init__(self):
        self.dbpool = adbapi.ConnectionPool('MySQLdb',
            db='database',
            user='username',
            passwd='password')

    def query(self, sql)
        self.dbpool.runInteraction(self._query, sql)

    def _query(self, tx, sql):
        tx.execute(sql)
        print tx.fetchone()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top