[mod_python] Hi, database connection queston

Gregory Trubetskoy grisha at modpython.org
Sun May 28 14:06:26 EST 2000


On Sun, 28 May 2000, Cary O'Brien wrote:

> 1) How are people handling persistant database connections?
[....]
>    It seems from the documentation that each request gets its own
>    subinterpreter.

Incorrect. Each directive directory and its subdirectories get its own
subinterpreter. All requests for files in the directory where the
Python*Handler directive was first encountered and its subdirectories are
in the same subinterpreter.

This behaviour can be altered via PythonInterpreter or
PythonInterPerDirectory directives.

As far as database connections - here how I used to do it. I had a module
called data.py (this is not the entire code, some parts are cut out, think
of it as an example rather than something ready to use. cfg contained
configuration). In your scripts, you would have something like:

import data
result = data._run_sql("select * from myteable")

And it would behave exactly as you describe - initiate the db connection
once and keep it for as long as the process lives. Of course, you end up
having as many connections as there are Apache processes, which you can
control by MaxClients directive.

-------------

import cfg
import mysqldb
import string

def _db_login(relogin = 0):
    """Login to the database
    """

    global DB_CONN

    if relogin:
        DB_CONN = mysqldb.mysqldb(cfg.DB_LOGIN)
        return DB_CONN
    else:
        try:
            return DB_CONN

        except NameError:

            DB_CONN = mysqldb.mysqldb(cfg.DB_LOGIN)
            return DB_CONN

def _run_sql( sql, n = 0, with_desc=0 ):
    """ Runs SQL on the server and returns result
    """

    db = _db_login()

    try:
        cur = db.cursor()
        rc = cur.execute(sql)

    except:
        # it's possible that mysql connection
        # timed out. Try one more time.
        db = _db_login(relogin = 1)
        cur = db.cursor()
        rc = cur.execute(sql)

    if string.upper(string.split(sql)[0]) in \
       ("SELECT", "SHOW", "DESC", "DESCRIBE"):
        if n:
            recset = cur.fetchmany(n)
        else:
            recset = cur.fetchall()

        if with_desc:
            return recset, cur.description
        else:
            return recset
    else:
        return rc

------------------

> 2) Re Zope.  There seems to be a ZPublisher interface.  Does this mean I can
>    run 30 apache processes against the same Zope database?  

As I already said on comp.lang.python - no.

>    How fine/course is
>    the locking on the zope database?  I asked this on the zope list a while
>    back and didn't get a good answer.

The first process will lock the database, and all the other ones will just
sit there and wait :-(

> 3) Does anyone have the bits to embed python calls in an html document a-la 
>    embperl/php/mod_dtcl? I know mixing logic with presentation is 
>    in general a bad idea, but having embedded code in documents is a fast
>    way to get things up and running.

You can use the Z DocuemntTemplate with ZHandler, it works pretty well.

HTH,

Grisha




More information about the Mod_python mailing list