I'm looking for information on how to correctly use mongoengine with Flask. Background info: Mongoengine uses a global connection object that doesn't work well with threads (see: http://goo.gl/wB0MZ and <goog_1683650618>http://goo.gl/QVDvU). As a fall back option, I am creating a mongodb connection before each request and closing the connection after the connection (using the before_request and after_request decorators). However, this doesn't seem to be an optimal method. So I am wondering if it is possible to use mongoengine with Flask ... without needing to create a connection for every single request? Much thanks. -Cheng-Han
I'm not too familiar with MongoEngine, so my opinion here might be wrong, Corrections are welcomed :) A global connection is actually acceptable since MongoEngine uses normal PyMongo connection, which is thread-safe and supports connection-pool, so as long as you disconnect/end_request properly, you are safe. The main problem from the thread you referred to might be gunicorn + pymongo. Since pymongo expected to be run in a threaded environment, it may not play nicely with greenlet or async worker. Daniel. On Mon, Aug 15, 2011 at 1:49 PM, Cheng-Han Lee <lee.chenghan@gmail.com>wrote: > I'm looking for information on how to correctly use mongoengine with > Flask. > > Background info: > Mongoengine uses a global connection object that doesn't work well with > threads (see: http://goo.gl/wB0MZ and <http://goog_1683650618> > http://goo.gl/QVDvU). > > As a fall back option, I am creating a mongodb connection before each > request and closing the connection after the connection (using the > before_request and after_request decorators). However, this doesn't seem to > be an optimal method. > > So I am wondering if it is possible to use mongoengine with Flask ... > without needing to create a connection for every single request? > > Much thanks. > > -Cheng-Han >
Hi, Daniel sums it up well, btw - we've written a Flask extension for mongoengine https://github.com/sbook/flask-mongoengine Has debugtoolbar support and wtform support. Ross On Mon, Aug 15, 2011 at 7:27 AM, Daniel , Dao Quang Minh <dqminh89@gmail.com > wrote: > I'm not too familiar with MongoEngine, so my opinion here might be wrong, > Corrections are welcomed :) > > A global connection is actually acceptable since MongoEngine uses normal > PyMongo connection, which is thread-safe and supports connection-pool, so as > long as you disconnect/end_request properly, you are safe. > The main problem from the thread you referred to might be gunicorn + > pymongo. Since pymongo expected to be run in a threaded environment, it may > not play nicely with greenlet or async worker. > > Daniel. > > On Mon, Aug 15, 2011 at 1:49 PM, Cheng-Han Lee <lee.chenghan@gmail.com>wrote: > >> I'm looking for information on how to correctly use mongoengine with >> Flask. >> >> Background info: >> Mongoengine uses a global connection object that doesn't work well with >> threads (see: http://goo.gl/wB0MZ and <http://goog_1683650618> >> http://goo.gl/QVDvU). >> >> As a fall back option, I am creating a mongodb connection before each >> request and closing the connection after the connection (using the >> before_request and after_request decorators). However, this doesn't seem to >> be an optimal method. >> >> So I am wondering if it is possible to use mongoengine with Flask ... >> without needing to create a connection for every single request? >> >> Much thanks. >> >> -Cheng-Han >> > >
Thanks for your help guys. Also, I looked through mongoengine's issues on github. Apparently, mongoengine had a bug that was causing this authentication issue ( https://github.com/hmarr/mongoengine/issues/151). i pulled the latest mongoengine dev build from github, and everyone works great now! -Cheng-Han On Mon, Aug 15, 2011 at 12:38 PM, Ross Lawley <ross.lawley@gmail.com> wrote: > Hi, > > Daniel sums it up well, btw - we've written a Flask extension for > mongoengine > > https://github.com/sbook/flask-mongoengine > > Has debugtoolbar support and wtform support. > > Ross > > > On Mon, Aug 15, 2011 at 7:27 AM, Daniel , Dao Quang Minh < > dqminh89@gmail.com> wrote: > >> I'm not too familiar with MongoEngine, so my opinion here might be wrong, >> Corrections are welcomed :) >> >> A global connection is actually acceptable since MongoEngine uses normal >> PyMongo connection, which is thread-safe and supports connection-pool, so as >> long as you disconnect/end_request properly, you are safe. >> The main problem from the thread you referred to might be gunicorn + >> pymongo. Since pymongo expected to be run in a threaded environment, it may >> not play nicely with greenlet or async worker. >> >> Daniel. >> >> On Mon, Aug 15, 2011 at 1:49 PM, Cheng-Han Lee <lee.chenghan@gmail.com>wrote: >> >>> I'm looking for information on how to correctly use mongoengine with >>> Flask. >>> >>> Background info: >>> Mongoengine uses a global connection object that doesn't work well with >>> threads (see: http://goo.gl/wB0MZ and <http://goog_1683650618> >>> http://goo.gl/QVDvU). >>> >>> As a fall back option, I am creating a mongodb connection before each >>> request and closing the connection after the connection (using the >>> before_request and after_request decorators). However, this doesn't seem to >>> be an optimal method. >>> >>> So I am wondering if it is possible to use mongoengine with Flask ... >>> without needing to create a connection for every single request? >>> >>> Much thanks. >>> >>> -Cheng-Han >>> >> >> >