The stream_to_handler() function streams the contents of a GridFS file to an HTTP client, using a tornado.web.RequestHandler. Hence it's Tornado-specific, but I implemented and tested it framework-agnostically to keep the door open for an asyncio equivalent feature (MOTOR-92).
Motor's framework-agnostic way to stream chunks from a GridFS file is:
while written < self.length:
chunk = yield self._framework.yieldable(self.read(self.chunk_size))
# ... etc ...
Yury Selivanov optimized how asyncio coroutines resolve Futures to values (pull 289) in a way that broke Motor's framework-compatibility layer starting in Python 3.4.4 and 3.5.1.
stream_to_handler, and any other Motor function I write from now on that resolves a Future to a value, must resolve it in two steps:
while written < self.length:
f = self._framework.yieldable(self.read(self.chunk_size))
yield f
chunk = f.result()
This is a minor bug since it only broke in asyncio, where this function won't be useful until and unless I implement MOTOR-92.