Motor 0.3.2 發佈,此版本相容 HTTP://www.aliyun.com/zixun/aggregation/13461.html">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
示例代碼:
?123456789101112131415161718192021222324252627282930313233from 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}) & nbsp; # 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)]) & nbsp; while (yield cursor.fetch_next): message = cursor.next_ object() self.write('<li>%s</li>' % message['msg']) & nbsp; # Iteration complete self.write('</ul>') & nbsp; self.finish()
Motor API
MotorClient – Connection to MongoDB
MotorReplicaSetClient – Connection to MongoDB replica set
MotorDatabase
MotorCollection
MotorCursor
Motor GridFS Classes
motor.web