Home Blog CV Projects Patterns Notes Book Colophon Search

An API with Trollius using Futures and yield from

22 Apr, 2014

I've been looking into Google App Engine again recently. One thing that is quite interesting is the interal design of the md_code(ndb) module. The docstring points at monocle as an inspirtation.

https://chromium.googlesource.com/external/googleappengine/python/+/69de4b85b39dc737634b0150a8e412730b8ed9e2/google/appengine/ext/ndb/tasklets.py

What is nice is that the API can be used syncronosuly, or asyncronously with futures, or using md_code(yield From). (There is no md_code(yield from) because it is Python 3).

In turn it is pretty clear from the design of Python 3.4's md_code(asyncio) module that some of these ideas have made it into its design. md_code(trollius) is the name of the Python 2.5+ version of the md_code(asyncio) codebase.

http://code.activestate.com/lists/python-ideas/19847/

I've been having a little play, and this is a simple API that allows code that looks blocking to use either futures or yield from both externally and internally. I quite like the approach. Although the code to set up and run the loop looks complicated, and the code inside the md_code(slow_operation()) function looks complicated, the use of the function by the user is simple.

Here is md_code(demo.py):

undivert(`../file/trollius_futures_yield_from/demo.py')

Run it like this:

virtualenv env
env/bin/pip install trollius
env/bin/python demo.py

undivert(`../file/mit-license.txt')

Copyright James Gardner 1996-2020 All Rights Reserved. Admin.