Motor tutorial

122 views
Skip to first unread message

A. Jesse Jiryu Davis

unread,
Oct 6, 2012, 5:59:44 PM10/6/12
to python-...@googlegroups.com
I wrote up a tutorial on Motor, my MongoDB driver for Tornado:

http://emptysquare.net/motor/pymongo/api/motor/tutorial.html

Let me know if it helps or hinders you, or if you can see ways to improve it.

aliane abdelouahab

unread,
Oct 6, 2012, 6:09:19 PM10/6/12
to Tornado Web Server
first one to answer :D
i'll make a good night reading this jewel :D
thank you ;)

aliane abdelouahab

unread,
Oct 6, 2012, 6:20:27 PM10/6/12
to Tornado Web Server
just as a contribution:

A single instance of MongoDB can support multiple independent
databases. From an open connection, you can get a reference to a
particular database with dot-notation or bracket-notation:
>>> db = connection.test_database
>>> db = connection['test_database']

the second one (connection['name_of_the_database']) is used when the
name of database dont respects python variable naming.

On 6 oct, 22:59, "A. Jesse Jiryu Davis" <ajesseda...@gmail.com> wrote:

aliane abdelouahab

unread,
Oct 6, 2012, 6:26:59 PM10/6/12
to Tornado Web Server
am sorry for asking this dumb question, but can i use this technique
(which is used in the examples of projects made by tornado users) to
make inheritence?

class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
user = self.get_secure_cookie("current_user") or False
if not user: return None
return tornado.escape.json_decode(user)

@property
def db(self):
if not hasattr(BaseHandler,"_db"):
_db = pymongo.Connection().tuxhub
return _db

@property
def fs(self):
if not hasattr(BaseHandler,"_fs"):
_fs = gridfs.GridFS(self.db)
return _fs

it will work in the same maneer in Motor? because i've seen a
parameter:

application = tornado.web.Application([
(r'/', MainHandler)
], db=db)

if i use the first technique, then i will add the db=db ?

On 6 oct, 22:59, "A. Jesse Jiryu Davis" <ajesseda...@gmail.com> wrote:

A. Jesse Jiryu Davis

unread,
Oct 7, 2012, 12:07:14 AM10/7/12
to python-...@googlegroups.com
No, because your properties are accessed synchronously.

So, this line:

   _fs = gridfs.GridFS(self.db)

... opens a GridFS object in a blocking style. The next line isn't executed until the object has been created. The IOLoop is blocked and no more requests can be processed until the GridFS object is created. In Motor, on the other hand, you need to open it asynchronously:

    fs = motor.MotorGridFS(self.db)
    fs.open(my_callback)


The object isn't "open" until the callback has been executed, some time later. See my GridFSHandler for an example of using MotorGridFS:

https://github.com/ajdavis/mongo-python-driver/blob/motor/motor/web.py

On the other hand, if you look at the code for the regular PyMongo gridfs.GridFS constructor, you'll see all it does is an ensure_index(), which (assuming the index is already created) is so fast that it's probably not a problem. *Reading* from a PyMongo GridFS object, however, will block the IOLoop until complete, which for large files *will* be a problem.

aliane abdelouahab

unread,
Oct 7, 2012, 4:18:48 AM10/7/12
to Tornado Web Server
so i'll avoid the classic way to make gridfs and pymongo inherit and
declare them in each class?

On 7 oct, 05:07, "A. Jesse Jiryu Davis" <ajesseda...@gmail.com> wrote:
> No, because your properties are accessed synchronously.
>
> So, this line:
>
>    _fs = gridfs.GridFS(self.db)
>
> ... opens a GridFS object in a *blocking* style. The next line isn't

A Jesse Jiryu Davis

unread,
Oct 7, 2012, 12:20:57 PM10/7/12
to python-...@googlegroups.com
I think you should just keep using PyMongo.

aliane abdelouahab

unread,
Oct 7, 2012, 5:12:43 PM10/7/12
to Tornado Web Server
yes you're right ;)
thank you :)

On 7 oct, 17:21, A Jesse Jiryu Davis <ajda...@cs.oberlin.edu> wrote:
> I think you should just keep using PyMongo.
>
> On Sun, Oct 7, 2012 at 1:18 AM, aliane abdelouahab
> <alabdeloua...@gmail.com>wrote:
Reply all
Reply to author
Forward
0 new messages