3 beginner question about Motor

88 views
Skip to first unread message

aliane abdelouahab

unread,
Nov 10, 2012, 3:29:51 PM11/10/12
to mongodb-user
hi, am sorry for those dumb questions:

1 - in motor documentation there is:

Warning
It is a common mistake to create a new connection object for every
request; this comes at a dire performance cost. Create the connection
when your application starts and reuse that one connection for the
lifetime of the process, as shown in these examples.
so, how many connections there is this example:

class BaseHandler(tornado.web.RequestHandler):
@property
def db(self):
if not hasattr(BaseHandler,"_db"):
_db = motor.MotorConnection().open_sync().mydb
return _db
@property
def fs(self):
if not hasattr(BaseHandler,"_fs"):
_fs = motor.MotorGridFS(self.db)
return _fs

class LoginHandler(BaseHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def post(self):
#make database search to seach users.

2 - i got error when trying this:

dbmail = yield motor.Op(self.db.users.find, {"_id":email})

File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg\tornado
\gen.py", line 389, in run
self.yield_point.start(self)
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\gen.py", line 243, in start
self.func(*self.args, **self.kwargs)
File "build\bdist.win-amd64\egg\motor\__init__.py", line 1048, in
find
"Pass a callback to next_object, each, to_list, count, or tail,"
InvalidOperation: Pass a callback to next_object, each, to_list,
count, or tail,not to find

and in the example i understand that i must make a blocking operation
and then call the gen?
from tornado import gen

@gen.engine
def get_some_documents(db):
cursor = db.collection.find().limit(10)
try:
documents = yield motor.Op(cursor.to_list)
return documents
except Exception, e:
print e

3 - why do i get error when using

self.db.users.find_one({"_id":email})

File "build\bdist.win-amd64\egg\motor\__init__.py", line 316, in
method
check_callable(callback, required=callback_required)
File "build\bdist.win-amd64\egg\motor\__init__.py", line 78, in
check_callable
raise TypeError("callable is required")
TypeError: callable is required

is find_one in motor different from find_one in pymongo ?

Ross Lawley

unread,
Nov 12, 2012, 8:14:09 AM11/12/12
to mongod...@googlegroups.com
Hi some answers inline:


On Saturday, 10 November 2012 20:30:05 UTC, aliane abdelouahab wrote:
hi, am sorry for those dumb questions:

1 - in motor documentation there is:

Warning
It is a common mistake to create a new connection object for every
request; this comes at a dire performance cost. Create the connection
when your application starts and reuse that one connection for the
lifetime of the process, as shown in these examples.
so, how many connections there is this example:

class BaseHandler(tornado.web.RequestHandler):
    @property
    def db(self):
        if not hasattr(BaseHandler,"_db"):
            _db = motor.MotorConnection().open_sync().mydb
            return _db
...

This doesnt work as expected as you never set _rs or _fs to the class BaseHandler.    A nicer pattern might be to passing it into the application: Here's an example: https://github.com/rozza/try-aggro/blob/master/server.py, so then you can refer to it in your application like so: db = self.application.settings['db']
 

2 - i got error when trying this:

dbmail = yield motor.Op(self.db.users.find, {"_id":email})


You need to provide callbacks for the cursor, so that the query is actually run* - see: http://emptysquare.net/motor/pymongo/api/motor/tutorial.html#querying-for-more-than-one-document. * motor.Op - actually runs the MongoDB operation and resumes the generator with the result of the operation when it is complete.
 
3 - why do i get error when using

self.db.users.find_one({"_id":email})
  ...
is find_one in motor different from find_one in pymongo ?


Ross

aliane abdelouahab

unread,
Nov 12, 2012, 9:41:41 AM11/12/12
to mongodb-user
thank you for the reply :)

> Hi some answers inline:
> > 1 - in motor documentation there is:
>
> > Warning
> > It is a common mistake to create a new connection object for every
> > request; this comes at a dire performance cost. Create the connection
> > when your application starts and reuse that one connection for the
> > lifetime of the process, as shown in these examples.
> > so, how many connections there is this example:
>
> > class BaseHandler(tornado.web.RequestHandler):
> >     @property
> >     def db(self):
> >         if not hasattr(BaseHandler,"_db"):
> >             _db = motor.MotorConnection().open_sync().mydb
> >             return _db
> > ...
>
> This doesnt work as expected as you never set _rs or _fs to the class
> BaseHandler.    A nicer pattern might be to passing it into the
> application: Here's an example:https://github.com/rozza/try-aggro/blob/master/server.py, so then you can
> refer to it in your application like so: db
> = self.application.settings['db']

this is because the first application i've learned is TuxHub, and he
used this technique to subclass handlers:

https://github.com/badubizzle/tuxhub/blob/master/handlers.py#L17

> You need to provide callbacks for the cursor, so that the query is actually
> run* -
> see:http://emptysquare.net/motor/pymongo/api/motor/tutorial.html#querying....
> * motor.Op - actually runs the MongoDB operation and resumes the generator
> with the result of the operation when it is complete.
>

still learning the async :(

> > 3 - why do i get error when using
>
> > self.db.users.find_one({"_id":email})
> >   ...
>
> is find_one in motor different from find_one in pymongo ?
>
>
>
> Yes it also is needs motor.Op to run the query eg:
>  http://emptysquare.net/motor/pymongo/api/motor/tutorial.html#getting-...
>

it is difficult to make transition! really difficult to a beginner or
there is a problem in my mind!


> Ross

thank you again :)
Reply all
Reply to author
Forward
0 new messages