微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Mongodb Motor MongoDB 的 Python 驱动

程序名称:Mongodb Motor

授权协议: Apache

操作系统: 跨平台

开发语言: Python

Mongodb Motor 介绍

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

安装:$ pip install motor

示例代码

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

Mongodb Motor 官网

https://motor.readthedocs.org/en/latest/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐