[Python-Dev] new buffer in python2.7

Kristján Valur Jónsson kristjan at ccpgames.com
Mon Nov 1 10:09:31 CET 2010


Ah, yes.  There are, in my case.  (why do I always seem to be doing stuff that is different from what you all are doing:)
The particular piece of code is from the chunked reader. It may be reading rather large chunks at a time (several lots of Kb.):

def recvchunk(socket):
    len = socket.unpack('i', recv_exactly(socket, 4))
    return recv_exactly(len)

#old style
def recv_exactly(socket, length):
    data = []
    while length:
        got = socket.receive(length)
        if not got: raise EOFError
        data.append(got)
        length -= len(got)
    return "".join(data)

#new style
def recv_exactly(socket, length):
    data = bytearray(length)
    view = memoryview(data)
    while length:
        got = socket.receive_into(view[-length:])
        if not got: raise EOFError
        length -= len(got)
    return data


Here I spot another optimzation oppertunity:  let memoryview[:] return self, since the object is immutable, I believe.
K

> -----Original Message-----
> From: "Martin v. Löwis" [mailto:martin at v.loewis.de]
> Sent: 1. nóvember 2010 14:22
> To: Kristján Valur Jónsson
> Cc: python-dev at python.org
> Subject: Re: [Python-Dev] new buffer in python2.7
> 
> 
> Assuming there are multiple recv calls. For a typical struct, all data
> will come out of the stream with a single recv. so no join will be
> necessary.
> 
> Regards,
> Martin



More information about the Python-Dev mailing list