使用tornado ioloop的异步mongodb gridfs
asyncgridfs的Python项目详细描述
AsyncGridFs
==
Tornado的Async Mongo GridFs
处理程序(tornado.web.requesthandler):
@property
def db(self):
如果不是hasattr(self,“_db”):
self._db=asyncMongo.client(pool_id='mydb',host='127.0.0.1',port=27017,maxcached=10,maxconnections=50,dbname='test')
返回self.\u db
@tornado.web.asynchronous
def get(self):
fid=self.get廑argument('fid')
fs=gridfs(self.db)
fs.get(objectid(fid),callback=self.\u on-get)
@tornado.web.asynchronous
def post(self):
f=self.request.files['imgfile'][0]
content=f.pop('body')
content\type=f.pop('content\u type')
filename=f.pop('filename')
fs.put(content,contentType=content廑type,filename=filename,callback=self.廑on廑post)
def廑on廑get(self,fileobj):
self.set廑header('content-type',fileobj['contentType'])
self.write(fileobj['data'])
self.finish()
def廑self.write(str(_id))
self.finish()
==
Tornado的Async Mongo GridFs
处理程序(tornado.web.requesthandler):
@property
def db(self):
如果不是hasattr(self,“_db”):
self._db=asyncMongo.client(pool_id='mydb',host='127.0.0.1',port=27017,maxcached=10,maxconnections=50,dbname='test')
返回self.\u db
@tornado.web.asynchronous
def get(self):
fid=self.get廑argument('fid')
fs=gridfs(self.db)
fs.get(objectid(fid),callback=self.\u on-get)
@tornado.web.asynchronous
def post(self):
f=self.request.files['imgfile'][0]
content=f.pop('body')
content\type=f.pop('content\u type')
filename=f.pop('filename')
def廑on廑get(self,fileobj):
self.set廑header('content-type',fileobj['contentType'])
self.write(fileobj['data'])
self.finish()
def廑self.write(str(_id))
self.finish()