Python sockets buffering

后端 未结 3 1834
无人共我
无人共我 2020-12-08 20:39

Let\'s say I want to read a line from a socket, using the standard socket module:

def read_line(s):
    ret = \'\'

    while True:
        c =          


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 21:07

    If you are concerned with performance and control the socket completely (you are not passing it into a library for example) then try implementing your own buffering in Python -- Python string.find and string.split and such can be amazingly fast.

    def linesplit(socket):
        buffer = socket.recv(4096)
        buffering = True
        while buffering:
            if "\n" in buffer:
                (line, buffer) = buffer.split("\n", 1)
                yield line + "\n"
            else:
                more = socket.recv(4096)
                if not more:
                    buffering = False
                else:
                    buffer += more
        if buffer:
            yield buffer
    

    If you expect the payload to consist of lines that are not too huge, that should run pretty fast, and avoid jumping through too many layers of function calls unnecessarily. I'd be interesting in knowing how this compares to file.readline() or using socket.recv(1).

提交回复
热议问题