facebook/tornado — pull request #940 by klaussfreire (2013-11-19 17:38:58+00:00)
Title: Speed up gen.corouting when yielding done Futures

When using gen.coroutine and yielding futures, when also using heavy caching, many of those futures are done upon returning, needing no involvement from the ioloop. But tornado.gen still defers the result by adding the callback to the ioloop, adding extra latency to the request (the callback is scheduled after other callbacks and thus not immediate).

This creates unnecessary latency on mostly-CPU-bound code that can sometimes block, but mostly doesn't.

To work around this, I'm currently using the following idiom:

x = yield future_returning_call()

-> turns into ->

f = future_returning_call()
if f.done():
x = f.result()
else:
x = yield f
del f

However, this results in ugly code.

The proposed patch makes tornado.gen itself avoid the ioloop when a future is done at yield time, making it thus more efficient under CPU-bound conditions.