title: How does 'yield' work in tornado when making an asynchronous call?
body: <p>Recently, I was learning <strong><em>Introduction to Tornado</em></strong>, and I came across the following code:</p>
  
  <pre><code>class IndexHandler(tornado.web.RequestHandler):
      @tornado.web.asynchronous
      @tornado.gen.engine
      def get(self):
          query = self.get_argument('q')
          client = tornado.httpclient.AsyncHTTPClient()
          response = yield tornado.gen.Task(client.fetch,
                  "http://search.twitter.com/search.json?" + \
                  urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100}))
          body = json.loads(response.body)
  
          [...omitted the following code...]
  </code></pre>
  
  <p>I used to learn that <code>yield</code> is the key word turning a common function into a generator,  and when it used in the form <code>other = yield foo</code> means, <strong>"yield foo and, when a value is sent to me, set other to that value."</strong> So I tried the following code in ipython:</p>
  
  <pre><code>In [1]: result = 'init'     #set a global variable
  
  In [2]: def test_yield():
     ...:     global result
     ...:     print 'start test...'
     ...:     result = yield 'foo'
     ...:     print 'end test...'
     ...:     
  
  In [3]: t = test_yield()
  
  In [4]: t.next()
  start test...
  Out[4]: 'foo'  #'foo' has been yield to the caller, and blocked
  </code></pre>
  
  <p>Now I printed the global varialbe <code>result</code>, and it still referred to string 'init':</p>
  
  <pre><code>In [5]: print result
  init
  </code></pre>
  
  <p>Then I called the <code>send()</code> method, and sent a new string to <code>yield</code>:</p>
  
  <pre><code>In [6]: t.send('new message')
  end test...
  ---------------------------------------------------------------------------
  StopIteration                             Traceback (most recent call last)
  /home/chiyu/&lt;ipython-input-6-b86312ad7d0e&gt; in &lt;module&gt;()
  ----&gt; 1 t.send('new message')
  
  StopIteration: 
  </code></pre>
  
  <p>As expected, a <code>StopIteration</code> was raised and output the string 'end test...', but now the global variable <code>result</code> has been changed:</p>
  
  <pre><code>In [7]: print result
  new message
  </code></pre>
  
  <p>Apperantly, the <code>yield</code> statement accepted the string when we called the <code>send()</code> method, and assigned the new string to the variable result.</p>
  
  <p><strong>MY QUESTION IS:</strong></p>
  
  <p>Back to the code showed on the top, according to this logic,</p>
  
  <pre><code>response = yield tornado.gen.Task(client.fetch,
                      "http://search.twitter.com/search.json?" + \
                      urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100}))
  </code></pre>
  
  <p>when method <code>client.fetch</code> returned, a <code>Task</code> instance would be created and <code>yield</code> to the caller, but the variable <code>response</code> on the left will recieve nothing because no <code>send()</code> method has been excuted. I got quite confused about this, and googled in vain.</p>
  
  <p><strong>I would be really appreciated for your explanations!</strong></p>