Nobody has an opinion on this branch? Is it the good approach to support asyncio in Trollius?
Victor 2014-06-11 17:58 GMT+02:00 Victor Stinner <[email protected]>: > Hi, > > I have a good news: I have a work-in-progress branch in Trollius which > should allow to use asyncio modules like aiohttp on Python 3.3 and > later. I'm interested by a review and feedback to see if I should > merge this branch or not. > > Example of Trollius using aiohttp (written for asyncio): > --- > import aiohttp > import asyncio > import trollius > > def get_body(url): > response = yield from aiohttp.request('GET', url) > body = yield from response.read_and_close() > print(body) > > loop = trollius.get_event_loop() > asyncio.set_event_loop(loop) > loop.run_until_complete(get_body('http://www.perdu.com/')) > loop.close() > --- > > > I decided to rename the Python module "asyncio" to "trollius" in the > version 0.3 of the Trollius project to support Python 3.4 and later. A > discussion followed this change because authors of modules have to > modify their code to support Trollius, and aiohttp author is not > interested to support Trollius for example (Andrew Svetlov likes > "yield-from", and I understand that ;-)). > > > I wrote a new "trollius_asyncio_interop" branch in Trollius to support > interoperability with asyncio. > > Attached patch is the current differences between trollius and > trollius_asyncio_interop branches. Summary of changes: > > - asyncio.set_event_loop() now accepts a Trollius event loop: the > hack(?) is to directly use asyncio.AbstractEventLoop in trollius if > the asyncio module is available > - an asyncio coroutine can yield from a trollius coroutine > - an trollius coroutine can yield an asyncio coroutine > > > See examples/interop_asyncio.py for an example mixing asyncio and > trollius coroutines. Link to the example: > > https://bitbucket.org/enovance/trollius/src/e637d42d59b114528c4467f94162d0f335e5df4d/examples/interop_asyncio.py?at=trollius_interop_asyncio > > > The nice thing is that asyncio code doesn't need to be modified! I > would prefer to not make asyncio uglier to support Python 2. asyncio > is supposed to be a shiny new feature of Python 3.4 and so don't care > of the old (dead) Python 2! > > The limitation is that the application must use a trollius event loop. > Asyncio event loops reject explicitly coroutines using "yield" > (instead of yield-from). > > I guess that something should be done for the event loop policy too. > > Trollius reuses directly the asyncio.AbstractEventLoop class. It might > cause issues if trollius and asyncio versions are different (if the > class is different). Since it's an abstract class and almost all > methods are overriden, I'm not sure that it's a real issue. > > Trollius project: > http://trollius.readthedocs.org/ > > Victor
