刚开始使用Tornado,不知道我哪里出错了,但我根本不能让它线程,这是我正在测试的代码。
import tornado.ioloop
import tornado.web
import time
from threading import Timer
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
t = Timer(5.0, self.on_response)
t.start()
print 'thread started'
def on_response(self):
self.write(str(time.time()))
self.finish()
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()如果我运行它,它可以工作,但它阻塞了整个then服务器5秒,所以如果我尝试快速连续加载该页面两次,它将打印‘线程启动’,等待5秒,第一个浏览器将加载,然后它将再次打印‘线程启动’,等待5秒,然后发送给第二个浏览器页面,因此总共花费了10秒。
即使运行tornado网站上的非阻塞示例,我也会遇到这个问题。有什么想法吗?
我尝试过Python2.6和2.7,easy_install的Tornado1.2.1。
发布于 2011-06-02 20:19:01
您发布的代码在Snow Leopard上使用Python 2.6.1可以像我预期的那样工作。您尝试过使用curl进行测试吗?:
curl --no-buffer localhost:8888 & curl --no-buffer localhost:8888发布于 2012-07-19 14:01:40
因此,这可能是浏览器/客户端指定的行为,使我们感到困惑。
https://stackoverflow.com/questions/6209624
复制相似问题