This question keeps coming up...
The solution is simple: write that logic as a separate method marked
with @coroutine, and fire it off in data_received() using async() (==
Task(), in this case). E.g.
def data_received(self, data):
asyncio.async(self.process_data(data))
@asyncio.coroutine
def process_data(self, data):
...stuff using yield from...
(The reason why this isn't built into the protocol is that if it was,
it would require alternate event loop implementations to deal with
coroutines.)
On Wed, Jan 22, 2014 at 6:26 AM, Eli Bendersky <[email protected]> wrote:
> Hello,
>
> I've been playing with some asyncio-related ideas and a question came up
> about using coroutines in the data_received method of asyncio Protocols.
>
> Here's a simple port of Twisted's IntNStringReceiverProtocol for asyncio:
>
> https://github.com/eliben/python3-samples/blob/master/async/asyncio-len-prefixed-string-protocol.py
>
> It's pretty straightforward, but it bothers me that data_received has to
> essentially manage a state machine, whereas I'd expect to be able to use
> coroutines there to just say something like the following in a loop:
>
> ... got length word
> ... now get 'length' bytes [yield from....]
> ... dispatch "string_received"
>
> Is this possible?
>
> Thanks in advance,
> Eli
>
--
--Guido van Rossum (python.org/~guido)