title: Tornado web asynchronous
body: <p>I am trying to play with this piece of code to understand @tornado.web.asynchronous. The code as intended should handle asynchronous web requests but it doesnt seem to work as intended. There are two end points:</p>
  
  <pre><code>1) http://localhost:5000/A  (This is the time consuming request and 
  takes a few seconds)
  2) http://localhost:5000/B (This is the fast request and takes no time to return.
  </code></pre>
  
  <p>However when I hit the browser to go to <a href="http://localhost:5000/A" rel="nofollow noreferrer">http://localhost:5000/A</a> and then while that is running go to <a href="http://localhost:5000/B" rel="nofollow noreferrer">http://localhost:5000/B</a> the second request is queued and runs only after A has finished.
  In other words one task is time consuming but it blocks the other faster task. What am I doing wrong?</p>
  
  <pre><code>import tornado.web
  from tornado.ioloop import IOLoop
  
  import sys, random, signal
  
  
  class TestHandler(tornado.web.RequestHandler):
  
      """
      In below function goes your time consuming task
      """
  
  
     def background_task(self):
        sm = 0
        for i in range(10 ** 8):
           sm = sm + 1
        return str(sm + random.randint(0, sm)) + "\n"
  
     @tornado.web.asynchronous
     def get(self):
        """ Request that asynchronously calls background task. """
        res = self.background_task()
        self.write(str(res))
        self.finish()
  
  
  class TestHandler2(tornado.web.RequestHandler):
  
     @tornado.web.asynchronous
     def get(self):
        self.write('Response from server: ' + str(random.randint(0, 100000)) + "\n")
        self.finish()
  
     def sigterm_handler(signal, frame):
       # save the state here or do whatever you want
       print('SIGTERM: got kill, exiting')
       sys.exit(0)
  
     def main(argv):
       signal.signal(signal.SIGTERM, sigterm_handler)
       try:
         if argv:
           print ":argv:", argv
           application = tornado.web.Application([
          (r"/A", TestHandler),
          (r"/B", TestHandler2),
          ])
  
          application.listen(5000)
          IOLoop.instance().start()
        except KeyboardInterrupt:
          print "Caught interrupt"
       except Exception as e:
          print e.message
       finally:
          print "App: exited"
  
  if __name__ == '__main__':
     sys.exit(main(sys.argv))
  </code></pre>