[英]In tornado ,how to make these functions async?
這是我的代碼:
#/test
class Test(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self):
res = yield self.inner()
self.write(res)
@tornado.gen.coroutine
def inner(self):
import time
time.sleep(15)
raise tornado.gen.Return('hello')
#/test_1
class Test1(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.coroutine
def get(self):
res = yield self.inner()
self.write(res)
@tornado.gen.coroutine
def inner(self):
raise tornado.gen.Return('hello test1')
當我獲取/ test然后獲取/ test_1時,/ test_1直到/ test響應后才響應,如何解決呢?
不要使用time.sleep()
。 time.sleep()
將阻止CPU循環。 相反,使用
yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout,
time.time() + sleep_seconds)
您遇到了兩個常見問題:
http://www.tornadoweb.org/en/stable/faq.html
首先,請不要在Tornado應用程序中使用time.sleep(),而應使用gen.sleep()。 其次,請注意,大多數瀏覽器不會同時從同一域中獲取兩個頁面:請使用“ curl”或“ wget”來測試您的應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.