Shannon -jj Behrens wrote:
> On 12/12/06, Philip Jenvey <[EMAIL PROTECTED]> wrote:
>> On Dec 12, 2006, at 2:32 PM, Mike Orr wrote:
>>> Hi. There's a discussion on the Pylons list about whether it's OK to
>>> put a thread-unsafe database connection in pylons.g, which is a
>>> paste.registry.StackedObjectProxy. I want to put a Durus connection
>>> there. My reading of the paste.registry docstring says this is OK.
>>> Could somebody confirm? Thanks.
>>>
>> The Globals object is global to your entire application. Pylons
>> creates one instance of it at startup and your application will see
>> that same Globals object as pylons.g in every thread. Your single
>> connection created in g would be shared among threads (not thread safe!)
>>
>> The thread safety in this case ensures a separate Pylons app deployed
>> under the same container/process would see its own Globals object
>> instead of your first apps' as pylons.g when concurrently serving a
>> request in a separate thread.
>>
>> If you want to recycle the connections in between different requests
>> you'll need a connection pool.
>
> I bet it's possible to setup the connection in the base class and
> store it in thread local storage. Hence, you'll have a) a place to
> put the code b) a way to make it one connection per thread. I'm sure
> it won't be hard to set this up, but I can't give you code from the
> top of my head.
I don't think you need something very Paste-specific. Here's my thought:
from paste.deploy import CONFIG
import threading
connections = threading.local()
def get_connection():
# Never call this at the module level!
filename = CONFIG['durus_file']
return get_connection_by_filename(filename)
def get_connection_by_filename(filename):
try:
conns = connections.by_filename
except AttributeError:
conns = connections.by_filename = {}
if filename not in conns:
conn = conns[filename] = make_connection(filename)
else:
conn = conns[filename]
return conn
This should lazily create persistent connections, one per thread. If
the threads die, the connection should be GC'd. You should also be able
to instantiate multiple instances of the same app in the same process if
they all point to different Durus files.
--
Ian Bicking | [EMAIL PROTECTED] | http://blog.ianbicking.org
_______________________________________________
Paste-users mailing list
[email protected]
http://webwareforpython.org/cgi-bin/mailman/listinfo/paste-users