On Mon, 23 Dec 2013 23:14:45 +0100
Tobias Oberstein
<[email protected]> wrote:
> 
> Twisted does do internal buffering and allows me to use the Twisted 
> coroutine analog "inlineCallbacks" within dataReceived. With asyncio, I 
> do the buffering myself and trigger a coroutine waiting on a Future to 
> be signaled.
> 
> In short: I can't call a coroutine from data_received, but I can call an 
> inlineCallbacks-decorated functions in dataReceived. This is a major 
> difference.

I'm willing to bet that you're doing something wrong here. Can you post
code snippets?

> >> I have now another issue. I am _extending_ support of a WebSocket
> >> framework (https://github.com/tavendo/AutobahnPython) from Twisted/Py2
> >> to Py3 and asyncio. I am _nearly_ there. 90% code is shared between
> >> Twisted and asyncio. But, in the shared code, I have multiple places:
> >>
> >>         if PY3 and self._coroutines:
> >>            yield from self.consumeData()
> >>         else:
> >>            self.consumeData()
> >
> > I don't really understand. If consumeData() isn't supposed to wait on
> > I/O, you don't need to "yield from" it.
> 
> consumeData() will ultimately call into user code, and I want that user 
> code to be able to be a co-routine.

So how it is supposed to work in the Python 2 case if you're simply
calling `self.consumeData()` without either yielding it or registering a
Deferred somewhere?

> > If you want to write code that's compatible with Python 2, you
> > shouldn't use "yield from" at all. Just write callback-style code,
> > rather than coroutine-style.
> 
> I am writing a framework, and I want to leave it to user's choice to use 
> coroutines.

That's completely orthogonal to whether your framework uses
coroutines for its internal implementation.

Coroutines are a convenience layer over plain callbacks; actually,
asyncio.Task is a subclass of asyncio.Future.

> > If you're a bit stuck, you can take a look at Obelus:
> > https://pypi.python.org/pypi/obelus/
> 
> Does that allow users to write app code as coroutines?

I've never tried, but there's no reason for it not to allow it. Some
glue code might be necessary, which I wasn't interested in writing.

Regards

Antoine.


Reply via email to