Am 23.12.2013 22:39, schrieb Antoine Pitrou:
On Mon, 23 Dec 2013 22:25:59 +0100
Tobias Oberstein
<[email protected]> wrote:
Am 23.12.2013 22:07, schrieb Guido van Rossum:
Protocol callbacks don't have a return value and can't (or shouldn't)
block. You can use async() or Task() to spawn a coroutine. You might

Thanks for making this clear. Just from looking at the signature of
data_received vs Twisted dataReceived, I first (naively) assumed similar
behavior. It's clear now, and in fact I got it working in the meantime
(using a deque() and a Future to signal).

I'm curious: what is the difference in behaviour between Twisted's
dataReceived and asyncio's data_received?

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 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.

In fact, I am using a maybe_yield idiom:

http://stackoverflow.com/a/20742763/884770

"similar" to Twisted's maybeDeferred.


I guess that works on Py3/Twisted (if Twisted would support Py3 some
time), but it breaks Py2 .. "yield from" is a syntax error, even if PY3
is False. There is no #ifndef in Python.

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.


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?

/Tobias


Regards

Antoine.



Reply via email to