I don't know. The threads are managed by PasteHTTPServer or mod_wsgi and Pylons isn't informed when they die. You can try the 'atexit' module or 'sys.exitfunc' callback.
Although I don't know how you can access the connections for other threads in the threadlocal. You could use a regular dict keyed by 'thread.ident'; that way you could access the connections for all threads. On Mon, Mar 22, 2010 at 5:11 PM, Kevin Wormington <[email protected]> wrote: > One other thing I though of; where would be the best place in the framework > to close the database connection when the thread/application dies? I'm not > sure if the low-level interface closes the connection automatically or not. > > 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. > > -- Mike Orr <[email protected]> -- 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.
