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

Any hint on what to do?

The problem is that to make coroutines work for user code, the whole call-chain _within_ the framework needs to be using "yield from".

/Tobias

also look at open_connection(), which has a lot of the logic you describe.

On Dec 23, 2013 7:52 AM, "Tobias Oberstein" <[email protected]
<mailto:[email protected]>> wrote:

    Hi,

    I am wondering how to call coroutines within
    asyncio.Protocol.data_received:

    
http://stackoverflow.com/questions/20746619/calling-coroutines-in-asyncio-protocol-data-received/20748559

    I am coming from Twisted, where doing asynchronous stuff within the
    corresponding callback ("dataReceived") is ok.

    Now, Twisted does internal buffering of data incoming from a socket.

    Am I supposed to do that myself and decouple the (asynchronous)
    processing via a receive queue and a Future to signal availability
    of received data?

    Like so:

    https://github.com/oberstet/scratchbox/blob/master/python/asyncio/client.py
    https://github.com/oberstet/scratchbox/blob/master/python/asyncio/server.py

    ?

    Thanks
    /Tobias


Reply via email to