我正在使用 tornado 框架,并且我的所有功能都是同步编写的。我将如何使这些异步?
class AuthLoginHandler(BaseHandler):
@tornado.web.asynchronous
def get(self):
self.render("login.html")
def post(self):
username = self.get_argument("UserName",strip = True)
password = self.get_argument("Password",strip = True)
user = auth_actions.login(username,password)
if not user:
self.redirect("/auth/login")
return
#user = user_actions.get_my_data(self.db,user['_id'])
self.set_secure_cookie("userdata", tornado.escape.json_encode(dumps(user.to_mongo())))
self.redirect("/")
def login(username,password,callback=None):
m = hashlib.md5()
m.update(password)
hashed = m.hexdigest()
login = User.objects(UserName=username.lower(),Password=hashed).exclude("Password","Wall","FriendsRequested","Friends","FriendsRequesting")
if login.first() is None:
login = User.objects(Email=username.lower(),Password=hashed).exclude("Password","Wall","FriendsRequested","Friends","FriendsRequesting")
if login.first() is None:
return None
logger.info(username + " has logged in")
if callback != None:
return callback(login.first())
return login.first()