title: Asynchronous COMET query with Tornado and Prototype
body: <p>I'm trying to write simple web application using Tornado and JS Prototype library. So, the client can execute long running job on server. I wish, that this job runs Asynchronously - so that others clients could view page and do some stuff there.</p>
  <p>Here's what I've got:</p>
  <pre><code>#!/usr/bin/env/ python
  
  import tornado.httpserver
  import tornado.ioloop
  import tornado.options
  import tornado.web
  from tornado.options import define, options
  
  import os
  import string
  from time import sleep
  from datetime import datetime
  
  define(&quot;port&quot;, default=8888, help=&quot;run on the given port&quot;, type=int)
  
  class MainHandler(tornado.web.RequestHandler):
      def get(self):
          self.render(&quot;templates/index.html&quot;, title=&quot;::Log watcher::&quot;, c_time=datetime.now())
  
  class LongHandler(tornado.web.RequestHandler):
      @tornado.web.asynchronous
      def get(self):
          self.wait_for_smth(callback=self.async_callback(self.on_finish))
          print(&quot;Exiting from async.&quot;)
          return
  
      def wait_for_smth(self, callback):
          t=0
          while (t &lt; 10):
              print &quot;Sleeping 2 second, t={0}&quot;.format(t)
              sleep(2)
              t += 1
          callback()
  
      def on_finish(self):
          print (&quot;inside finish&quot;)
          self.write(&quot;Long running job complete&quot;)
          self.finish()
  
  
  
  def main():
      tornado.options.parse_command_line()
  
      settings = {
          &quot;static_path&quot;: os.path.join(os.path.dirname(__file__), &quot;static&quot;),
          }
  
      application = tornado.web.Application([
          (r&quot;/&quot;, MainHandler),
          (r&quot;/longPolling&quot;, LongHandler)
          ], **settings
      )
      http_server = tornado.httpserver.HTTPServer(application)
      http_server.listen(options.port)
      tornado.ioloop.IOLoop.instance().start()
  
  
  if __name__ == &quot;__main__&quot;:
      main()
  </code></pre>
  <p>This is server part. It has main view (shows little greeting, current server time and url for ajax query, that executes long running job. If you press a button, a long running job executes. And server hangs :( I can't view no pages, while this job is running.
  Here is template page:</p>
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;{{ title }}&lt;/title&gt;
  
      &lt;script type=&quot;text/javascript&quot; language=&quot;JavaScript&quot; src=&quot;{{ static_url(&quot;js/prototype.js&quot;)}}&quot;&gt;&lt;/script&gt;
  
  
      &lt;script type='text/javascript' language='JavaScript'&gt;
          offset=0
          last_read=0
  
          function test(){
              new Ajax.Request(&quot;http://172.22.22.22:8888/longPolling&quot;,
              {
                  method:&quot;get&quot;,
                  asynchronous:true,
                  onSuccess: function (transport){
                      alert(transport.responseText);
                  }
              })
          }
  
          
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      Current time is {{c_time}}
      &lt;br&gt;
      &lt;input type=&quot;button&quot; value=&quot;Test&quot; onclick=&quot;test();&quot;/&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>what am I doing wrong? How can implement long pooling, using Tornado and Prototype (or jQuery)</p>
  <p>PS: I have looked at Chat example, but it too complicated. Can't understand how it works :(</p>
  <p>PSS Download full <a href="http://www.mediafire.com/?ilgzdzrw24y" rel="nofollow noreferrer">example</a></p>