L-R*_*L-R 0 python sorting tornado mongodb pymongo
想要对一个简单的查询进行排序,但不确定它如何与"gen.task"一起使用,因为它将arg1和param的方法作为arg2.
这不仅仅是很好的:
response, error = yield gen.Task(db.client().collection.find, {"user_id":user_id})
if response:
#blablabla
Run Code Online (Sandbox Code Playgroud)
但那我怎么给它sort()?
更新:现在抛出'回调必须可调用'错误.这似乎是Tornado现在的另一个问题.
def findsort(self, find, callback):
return callback(db.client().collection.find(find).sort({"myfield":1}))
@gen.engine
def anotherfunction(self):
response, error = yield gen.Task(self.findsort, {"user_id":user_id})
Run Code Online (Sandbox Code Playgroud)
使用asyncmongo,它完美搭配gen.
杂耍之后你会得到这样的东西:
DB = asyncmongo.Client()
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@gen.engine
def get(self):
result, error = yield gen.Task(DB.collection.find, {}, limit=50, sort=[('myfield', 1)])
Run Code Online (Sandbox Code Playgroud)
关于'回调必须是可调用的'.当使用时gen- 总是在函数中描述+1参数,这被称为gen.Task.
def findsort(self, find, params, callback): #here we recieve self + 3 args, if we remove params - callback will contain {"user_id":user_id}
return callback(db.client().collection.find(find).sort({"myfield":1}))
@gen.engine
def anotherfunction(self):
response, error = yield gen.Task(self.findsort, {"user_id":user_id}) #we see 2 args, but it passes 3 args to findsort
Run Code Online (Sandbox Code Playgroud)