How would one use MongoEngine with Pyramid?

237 views
Skip to first unread message

Seth

unread,
Mar 25, 2011, 2:59:33 PM3/25/11
to pylons-...@googlegroups.com
I'm looking for information on how to correctly use mongoengine with Pyramid. Due to the fact that mongoengine uses a global connection object that doesn't work well with threads (see http://goo.gl/wB0MZ), the default mongodb setup (as outlined in the Pyramid Cookbook here: http://goo.gl/N94YJ) doesn't really work.

How would one use mongoengine with Pyramid without having to setup a connection for *every single request* using the request callback?

Thanks,
Seth

[Note: This is a re-post from MongoEngine Users, which doesn't seem to be active so I'm posting here instead and hoping that the Pyramid crew can be more helpful.]

oO

unread,
Mar 25, 2011, 6:49:31 PM3/25/11
to pylons-...@googlegroups.com
I'm not using MongoEngine, so I don't know about the particulars. I am however using PyMongo, and I'm using a different setup from the cookbook.

I setup the mongo connection as part of the RootFactory object (the one in config that returns the root resource)
When creating the root object, the root factory adds the already existing connection to the root resource root.db
views can always access the resource context, and the resource context can always access the root, and therefore the mongo db.

class MongoRootFactory(object):
    """Root Factory with a MongoDB connection"""
   
    def __init__(self, settings):
        """Initialize the factory by creating the mongo connection"""
        self.__mongo_uri = settings['mongo.uri']
        self.__mongo_db = settings['mongo.db']
        #Create the MongoDB connection   
        self.connection = pymongo.Connection(self.__mongo_uri)
        self.db = self.connection[ self.__mongo_db ]
        self.fs = GridFS(self.db)
        #Add the manipulator
        self.db.add_son_manipulator( AutoDateManipulator() )
   
    def __call__(self, request):
        """Return the root of the resource hierarchy"""
        #Create the root resource if it doesn't exists.
        if self.root == None:
            self.root = self.create_root()
        #Update the request object so the resource can access it.
        self.root.request = request
        self.root.db = self.db
        self.root.fs = self.fs

        return self.root

    def create_root(self):
        #log.debug("RootFactory: create the root object")
        return Root()



The RootFactory object lives inside the application, so the connection doesn't get created/closed for every request. I also find that the DB connection is something that should belong to the resources, not the request. (and you can always get it from the request using request.root.db

oO

Zak

unread,
Aug 11, 2012, 12:13:26 AM8/11/12
to pylons-...@googlegroups.com
What is AutoDateManipulator?
Message has been deleted
Message has been deleted

Takahiro Fujiwara

unread,
Aug 11, 2012, 1:17:02 AM8/11/12
to pylons-...@googlegroups.com
Hi, Im using mongoengine as mongodb connection wrapper too.

If you want to init connection for mongoengine, you can call connect() 
on your application main function.

In your application main::

    from mongoengine import connect as mongo_connect

    def main(global_config, **settings):
        # Register and connect databases.
        mongo_connect(
            settings['mongodb.database'],
            host=settings['mongodb.host'],
            port=int(settings['mongodb.port']))
        
… other initialization code …

I think it is best way to manage connection of databases.


-- 
Takahiro Fujiwara
Twitter: @tfmagician
Mail: tfmag...@gmail.com

On Saturday, August 11, 2012 at 1:35 PM, Zak wrote:

I'm receiving an error: global name Root is not defined


On Friday, March 25, 2011 5:49:31 PM UTC-5, oO wrote:
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/CX676Fb4qsAJ.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.

Reply all
Reply to author
Forward
0 new messages