2017-07-31 181 views
2

我用Tornado創建了一個登錄頁面,它對同步方法起作用。現在我想使它成爲異步。那麼什麼是改變我應該做下面的代碼:異步登錄龍捲風

import tornado.ioloop 
import tornado.web 
import http 
import time 

class BaseHandler(tornado.web.RequestHandler): 
    def get_current_user(self): 
     return self.get_secure_cookie("user") 

class MainHandler(BaseHandler): 
    def get(self): 
     if not self.current_user: 
      self.redirect("/login") 
      return 
     name = tornado.escape.xhtml_escape(self.current_user) 
     self.write('<html>' 
    '<head> ' 
    '<title>WELCOME </title>' 
    '</head>' 
    '<body style="background:orange;"' 

    '<div align="center">' 
    '<div style="margin-top:200px;margin-bottom:10px;">' 
    '<span style="width:500px;color:black;font-size:30px;font-weight:bold;">WELCOME </span>' 
    '</div>' 
    '<div style="margin-bottom:5px;">'"Hello, " + name) 

class LoginHandler(BaseHandler): 
    def get(self): 
     self.write('<html><body><form action="/login" method="post">' 
        '<div><span style="width:100px;;height: 500px;color:black;font-size:60;font-weight:bold;">' 
        'LOGIN' 
        '<div><span style="width:100px;;height: 500px;color:purple;font-size:30;font-weight:bold;">' 
        'NAME <input type="text" name="name">' 
        '<div><span style="width:100px;height: 500px;color:purple;font-size:30;font-weight:bold;">PASSWORD</span><input style="width:150px;" type="password" name="password" id="password" value="">' 
        '</div>'     
        '<input type="submit" value="Sign in">' 
        '</form></body></html>') 

    def post(self): 
     self.set_secure_cookie("user", self.get_argument("name")) 
     time.sleep(10) 
     self.redirect("/") 

application = tornado.web.Application([ 
    (r"/", MainHandler), 
    (r"/login", LoginHandler), 
], cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__") 

application.listen(5000) 
tornado.ioloop.IOLoop.current().start() 

在我的代碼有三個類BaseHandlerMainHandlerLoginHandler。任何幫助將不勝感激。

回答

0

使用async/await符號在這裏,可能適應適合您的Python版本:

class BaseHandler(tornado.web.RequestHandler): 
    async def get_current_user(self): 
     return await some_async_operation() 

    async def prepare(self): 
     self.current_user = await self.get_current_user() 

龍捲風不會叫get_current_user異步開箱即用,但它會履行異步prepare方法,這樣你就可以填充self.current_user有。如果您想暫停了一段時間來證明自己,這是無阻塞,加上from tornado import gen並嘗試方法

http://www.tornadoweb.org/en/stable/faq.html

+0

感謝您helping.But當我執行代碼時,出現錯誤:500。 – Erina

+0

你必須檢查你的錯誤日誌/在這裏激活調試,500可能是很多事情。 – deceze

+0

哦好吧,所以上面提到的變化應該理想地使代碼異步工作。是嗎? – Erina

0

不要叫「睡眠」龍捲風方法:

async def post(self): 
    self.set_secure_cookie("user", self.get_argument("name")) 
    yield gen.sleep(10) 
    self.redirect("/") 

或者在Python 2:

@gen.coroutine 
def post(self): 
    self.set_secure_cookie("user", self.get_argument("name")) 
    yield gen.sleep(10) 
    self.redirect("/") 
+0

我收到以下錯誤,當我嘗試yield gen.sleep:引發BadYieldError(「產生未知對象%r」%(已產生,)) tornado.gen.BadYieldError:產生未知對象 錯誤:tornado.access:500 POST/login(:: 1)8.00ms – Erina

+0

代碼在更改yield等待時有效。 – Erina

+0

有沒有辦法使這個代碼異步。通過改變func。並通過使用'@return_future def future_func(arg1,arg2,callback): #做東西(可能是異步的) 回調(結果) @gen。引擎 def caller(callback): yield future_func(arg1,arg2) callback() – Erina