你好,游客 登录
背景:
阅读新闻

Motor 0.3.2 发布,MongoDB 的 Python 驱动

[日期:2014-07-22] 来源:oschina  作者:oschina [字体: ]

Motor 0.3.2 发布,此版本兼容 MongoDB 2.2,2.4 和 2.6,最低要求 PyMongo 2.7.1。

此版本修复了在 "copy_database" 方法的 socket 泄漏,重写了 "Let Us Now Praise ResourceWarnings" 里面的问题和 bug。

获得最新版本:pip install --upgrade motor。更多内容请看这里。

Motor 为 Tornado 提供了一个基于回调和 Future 机制的非堵塞的 MongoDB 驱动程序。Motor 封装了 PyMongo

安装:$ pip install motor

示例代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from tornado import gen
  class NewMessageHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @gen.coroutine
    def post(self):
        """Insert a message."""
        msg = self.get_argument('msg')
        db = self.settings['db']
   
        # insert() returns a Future. Yield the Future to get the result.         result = yield db.messages.insert({'msg': msg})
   
        # Success         self.redirect('/')
   
  class MessagesHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @gen.coroutine
    def get(self):
        """Display all messages."""
        self.write('<a href="/compose">Compose a message</a><br>')
        self.write('<ul>')
        db = self.settings['db']
        cursor = db.messages.find().sort([('_id'-1)])
        while (yield cursor.fetch_next):
            message = cursor.next_object()
            self.write('<li>%s</li>' % message['msg'])
   
        # Iteration complete         self.write('</ul>')
        self.finish()

Motor API

 

 





收藏 推荐 打印 | 录入: | 阅读:
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数
点评:
       
评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款