Iterating a loop using await or yield causes error
<p>I come from the land of <code>Twisted</code>/<a href="https://github.com/twisted/klein" rel="nofollow"><code>Klein</code></a>. I come in peace and to ask for <code>Tornado</code> help.  I'm investigating Tornado and how its take on async differs from Twisted.  Twisted has something similar to <code>gen.coroutine</code> which is <a href="http://twistedmatrix.com/documents/current/core/howto/defer-intro.html#inline-callbacks-using-yield" rel="nofollow"><code>defer.inlineCallbacks</code></a> and I'm able to write async code like this:</p>

<p><em>kleinsample.py</em></p>

<pre><code>@app.route('/endpoint/&lt;int:n&gt;')
@defer.inlineCallbacks
def myRoute(request, n):
    jsonlist = []
    for i in range(n):
        yield jsonlist.append({'id': i})
    return json.dumps(jsonlist)
</code></pre>

<p>curl cmd:</p>

<pre><code>curl localhost:9000/json/2000
</code></pre>

<p>This endpoint will create a JSON string with <code>n</code> number of elements.  <code>n</code> can be small or very big.  I'm able to break it up in Twisted such that the event loop won't block using <code>yield</code>.  Now here's how I tried to convert this into Tornado:</p>

<p><em>tornadosample.py</em></p>

<pre><code>async def get(self, n):
    jsonlist = []
    for i in range(n):
        await gen.Task(jsonlist.append, {'id': i})    # exception here
    self.write(json.dumps(jsonlist))
</code></pre>

<p>The traceback:</p>

<pre><code> TypeError: append() takes no keyword arguments
</code></pre>

<p>I'm confused about what I'm supposed to do to properly iterate each element in the loop so that the event loop doesn't get blocked.  Does anyone know the "Tornado" way of doing this?</p>

<p>You cannot and must not await <code>append</code>, since it isn't a coroutine and doesn't return a Future. If you want to occasionally yield to allow other coroutines to proceed using Tornado's event loop, await <a href="http://www.tornadoweb.org/en/stable/gen.html#tornado.gen.moment" rel="nofollow">gen.moment</a>.</p>

<pre><code>from tornado import gen

async def get(self, n):
    jsonlist = []
    for i in range(n):
        jsonlist.append({'id': i})
        if not i % 1000:  # Yield control for a moment every 1k ops
           await gen.moment
    return json.dumps(jsonlist)
</code></pre>

<p>That said, unless this function is <em>extremely</em> CPU-intensive and requires hundreds of milliseconds or more to complete, you're probably better off just doing all your computation at once instead of taking multiple trips through the event loop before your function returns.</p>

python python-3.x asynchronous tornado coroutine