Sweet, that is just what I was looking for.

Thanks,

Kevin

Mike Orr wrote:
On Mon, Mar 22, 2010 at 4:18 PM, Kevin Wormington <[email protected]> wrote:
I'm not wanting to use sqlalchemy, just the low level database interface.  I
have a working example with sqlalchemy but I prefer to use the low level
interface so I need to make a db connection handle thread safe.  I don't
really care whether the connections are pooled.

You can use Python's threadlocal module. I did this for an object
database connection. I would put it under 'app_globals' so it's both
thread safe and application-instance safe.  You could do something
like this:

# In myapp/lib/app_globals.py
import threading
class Globals(object):
    def __init__(self):
        self.locals = threading.local()

    def get_db_connection(self):
        """Return the thread's database connection, creating it if it
doesn't exist."""
        conn = getattr(self.locals, "db_connection", None)
        if not conn:
            conn = CREATE_CONNECTION()
            self.locals.db_connection = conn
        return conn

# Wherever you need it
from pylons import app_globals
conn = app_globals.get_db_connection()


--
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to