title: how to debug Tornado async operation
body: <p>I am new to Tornado framework, and according to the link <a href="http://www.tornadoweb.org/en/stable/guide/async.html" rel="nofollow">Asynchronous and non-Blocking I/O</a>, I wrote some demo code as below. Unfortunately, the sync http client works, but async http client not. It looks like, the callback function that I passed to AsyncHTTPClient.fetch never has the chance to run. So my question is:</p>
  
  <ul>
  <li>Why tornado's async API not work for me?</li>
  <li>How should I debug this kind of problem? Set a break-point to my callback function is useless because it never has chance to run.</li>
  </ul>
  
  <p>Any help is great appreciated. Below is my demo code:</p>
  
  <pre><code>from tornado.httpclient import AsyncHTTPClient
  from tornado.httpclient import HTTPClient
  import time
  
  myUrl = 'a http url serving RESTful service'
  
  
  def async_fetch(url, callback):
      http_client = AsyncHTTPClient()
      def handle_test(response):
          callback(response.body)
      http_client.fetch(url, handle_test)
  
  
  def sync_fetch(url):
      http_client = HTTPClient()
      response = http_client.fetch(url)
      return response.body
  
  
  def printResponse(data):
      print("response is:" + data)
  
  
  def main():
      sync_fetch(myUrl)   #this works
      async_fetch(myUrl, printResponse)  #this not work
  
  
  if __name__ == '__main__':
      main()
      print("begin of sleep!")
      time.sleep(2)
      print("end of sleep!")
  </code></pre>