[openstack-dev] Asynchrounous programming: replace eventlet with asyncio

victor stinner victor.stinner at enovance.com
Wed Feb 5 12:44:14 UTC 2014


Hi,

Thierry Carrez wrote:
> > The problem is that the asyncio module was written for Python 3.3, whereas
> > OpenStack is not fully Python 3 compatible (yet). To easy the transition I
> > have ported asyncio on Python 2, it's the new Trollis project which
> > supports Python 2.6-3.4:
> >    https://bitbucket.org/enovance/trollius
> 
> How much code from asyncio did you reuse ? How deep was the porting
> effort ? Is the port maintainable as asyncio gets more bugfixes over time ?

Technically, Trollius is a branch of the Tulip project. I host the repository on Bitbucket, whereas Tulip is hosted at code.google.com. I use "hg merge" to retrieve last changes from Tulip into Trollius.

Differences between Trollius and Tulip show how much work has been done between Python 2.6 and 3.3 :-) Some examples:

- classes must inherit from object in Python 2.6 to be "new-style" classes (it's not more needed in Python 3),
- "{}".format() must be replaced with "{0}".format(),
- IOError/OSError exceptions have been reworked and now have specialized subclasses in Python 3.3 (I reimplemented them for Python 2.6),
- etc.

But most of the code is still the same between Tulip and Trollius. In my opinion, the major difference is that Tulip uses "yield from" wheras Trollius uses "yield", which imply subtle differences in the module iteself. You may not notice them if you use Trollius, but the implementation is a little bit different because of that (differences are limited to the asyncio/tasks.py file).

I'm working actively on Tulip (asyncio). We are fixing last bugs before the release of Python 3.4, scheduled for March 16, 2014. So I track changes in Tulip and I will "port" them into Trollius.

> > The Trollius API is the same than asyncio, the main difference is the
> > syntax in coroutines: "yield from task" must be written "yield task", and
> > "return value" must be written "raise Return(value)".
> 
> Could we use a helper library (like six) to have the same syntax in Py2
> and Py3 ? Something like "from six.asyncio import yield_from,
> return_task" and use those functions for py2/py3 compatible syntax ?

You can use Trollius with Python 3 (I tested it on Python 3.2, 3.3 and 3.4), so use "yield" syntax on Python 2 and Python 3.

Guido van Rossum proposed to use "yield From(future)" in Trollius, so it would be easier to port Trollius code (yield) on Tulip (yield from). Since OpenStack is not going to drop Python 2 support, I don't think that it's really useful (for OpenStack).

Victor



More information about the OpenStack-dev mailing list