This worked for me fine for another non-Pyramid app:
class MongoModel(object):
__dbconn = None
def __init__(self, properties=None):
self._id = None
if properties:
for k,v in properties.iteritems():
setattr(self, k, v if k != '_id' else
pymongo.objectid.ObjectId(v))
def collection(self):
if MongoModel.__dbconn is None:
MongoModel.__dbconn =
pymongo.Connection(host=conf["mongo.sets"].split(" "))[conf["mongo.db"]]
return MongoModel.__dbconn[self._collection]
...
This class also exposes the collection methods which internally call
those methods on self.collection() ... Then each model class would
inherit from MongoModel (and specify _collection argument in its
__init__ method).
However this is not threadsafe (for some other reasons I used prefork
process-only flup), so I suppose you'd need to make a threadlocal class
argument which contains __dbconn.
Since MongoDB is not transactional, I don't think you'll need middleware
to hook into request-response flow for the purposes of handling
autocommits or rollbacks, but ymmv.
.oO V Oo.
On 03/16/2011 06:38 PM, Stephen Lacy wrote:
Since mod_wsgi uses long-lived processes, I've decided to keep the
connection open for the life of the process (and auto-reconnect if
there's a failure).
I'm actually a little surprised that people here are connecting &
disconnecting with each request, as it feels unnecessary. Is there
some disadvantage of long-lived connections? Of course, it likely
varies per database, but FWIW, I'm using MongoDB as well.
Steve
--
You received this message because you are subscribed to the Google Groups
"pylons-devel" group.
To post to this group, send email to pylons-devel@googlegroups.com.
To unsubscribe from this group, send email to
pylons-devel+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/pylons-devel?hl=en.