I think your problem is lack of understand of how binary protocols work.
It is not an asyncio question (you would have the same issues with reading
from plain old sockets).

Basically, your protocol should allow you to understand how big a message
is just by reading the first few bytes of the message.  Another approach is
for the protocol to have markers telling you where each message ends.

Looking at your example, it sounds like you have the second approach.  It
is less efficient, but it can be done.  Basically, read char by char until
you find \x01, e.g.:

data = []
while True:
    c = yield from stream.readexactly(1)
    if c == b'\x01':
        break
message = b''.join(data)
# process this message

If you are designing a protocol, rather than just parse it, I advise you to
follow a Type/Length/Value structure, as it is easier to read and extend,
see https://en.wikipedia.org/wiki/Type-length-value


On 31 July 2015 at 17:40, Wellington Cordeiro <[email protected]> wrote:

> I'm not sure I entirely follow though, if I don't know the size of the
> response ahead of time, how will readexactly(N) help me?
>
> On Friday, July 31, 2015 at 10:12:29 AM UTC-6, Guido van Rossum wrote:
>>
>> Perhaps better to use readexactly(N), which raises EOF instead of
>> returning fewer than N bytes if it hits EOF early.
>>
>> On Fri, Jul 31, 2015 at 5:02 PM, Victor Stinner <[email protected]>
>> wrote:
>>
>>> 2015-07-31 6:56 GMT+02:00 Luciano Ramalho <[email protected]>:
>>> > It seems to me you can't use .read() with no arguments to read data
>>> > that is not line-oriented and is not the whole transmission either.
>>> > You must use .read(N), where N is a number of bytes. Then you parse
>>> > what you get and decide on a suitable value of N for the next read.
>>> > Rinse and repeat.
>>>
>>> Exactly.
>>>
>>> Victor
>>>
>>
>>
>>
>> --
>> --Guido van Rossum (python.org/~guido)
>>
>


-- 
Gustavo J. A. M. Carneiro
Gambit Research
"The universe is always one step beyond logic." -- Frank Herbert

Reply via email to