Frage

Can someone help me solve this problem. Here is my code:

class Handler(RequestHandler):
@asynchronous    
def get(self):
    res = 'result '
    _t = threading.Thread(target=self._thread, args=(res,))
    print _t, time.time()
    _t.start()

def _thread(self, response):
    time.sleep(5)
    IOLoop.instance().add_callback(callback=lambda: self.print_response(response))

def print_response(self, _response):
    self.write(_response)
    self.finish()

application = Application([
     (r'/', Handler),
])
if __name__ == '__main__':
application.listen(8889)
    IOLoop.instance().start()

On browser, visit localhost:8889 in one tab and localhost:8889 in another: I'll see that “result” is not printed in the second tab until the first one has finished, after 5 seconds. I think I was created 2 threads parallel processing, and when finished, add_callback result main loop. Tab2 should have results shortly after tab1 finished??? If I copy Handle class to Handle1 class, adding route r'/1', Handle1. Try again, localhost:8889 and localhost:8889/1 ---> It's will ok. Anyone can explain to me this problem and how to sloved it. Thank you!

War es hilfreich?

Lösung

It's not tornado, it's the browser. Browsers don't like making multiple requests for the same thing, so they won't send the second request until the first has finished. If you use two different browsers (or different urls), you'll see that it works.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top