MongoDB的異步Python驅動:Motor

jopen 10年前發布 | 35K 次閱讀 Motor MongoDB NoSQL數據庫

Motor是一個適用于PythonTornado 應用,功能完整,非阻塞的 MongoDB 驅動器。Motor 封裝自 PyMongo

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()</pre> <p><strong>項目主頁:</strong><a href="http://www.baiduhome.net/lib/view/home/1397782973046" rel="nofollow" target="_blank">http://www.baiduhome.net/lib/view/home/1397782973046</a></p>
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!