Can't get Tornado webserver to thread code
Just getting started with Tornado, have no idea what I'm getting wrong but I can't get it to thread at all, here's the code I'm testing with.
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()
If I run this, it works, but it blocks the entire webserver for 5 seconds, so if I try to load this page twice in quick succession, it will print 'thread started', wait 5 seconds, the first browser will load, then it will print 'threa开发者_StackOverflow中文版d started' again, wait another 5 seconds, and then send the second browser the page, thus taking a total of 10 seconds.
Even running the non-blocking example from the tornado website, I run into this issue. Any ideas?
Tried with python 2.6 and 2.7, Tornado 1.2.1 from easy_install.
The code you posted works as expected for me, with Python 2.6.1 on Snow Leopard. Have you tried testing with curl?:
curl --no-buffer localhost:8888 & curl --no-buffer localhost:8888
- I tried with chrome in 2 tabs. that takes about 10 seconds.
- I tried with IE9 in 2 tabs. that takes about 5 seconds.
- write a web page, send 2 AJAX requests to the URL one by one, (with meaningless query parameters to avoid browser cache) , that takes about 5 seconds.
- as you found, curl works around 5 seconds too.
so, that might be a browser/client specified behavior that making us confused.
精彩评论