简体   繁体   English

Python TCP套接字recv(1)与recv(n)的效率

[英]Efficiency of Python TCP socket recv(1) vs recv(n)

I'm building a TCP application which uses a newline \\n to separate messages of unknown length (but typically < 64 bytes). 我正在构建一个TCP应用程序,该应用程序使用换行符\\n分隔未知长度的消息(但通常小于64个字节)。 I'm finding this article to be very useful. 我发现本文非常有用。

Is the best (ie most efficient / fastest) approach to recv one character at a time and check if its a newline or recv a larger buffer and store "leftovers" in the class? 是最好的(即最有效/最快的)方法来一次recv一个字符并检查它是否是换行符或recv更大的缓冲区并在类中存储“剩余”?

One at a time 一次一个

def __recv_until_newline(self):
    message = ""
    while True:
        chunk = self.socket.recv(1)
        if chunk == "" or chunk == "\n":
            break
        message += chunk
    return message

Maintain a buffer 保持缓冲

def __recv_until_newline(self):
    """ self.__buffer holds previously received message parts"""
    message = self.__buffer
    while True:
        chunk = self.socket.recv(64)
        message += chunk
        if chunk == "" or "\n" in message:
            break

    # something a bit more intelligent than this in reality, but you get the idea
    messages = message.split('\n')
    self.__buffer = "".join([x for x in message[1:]])
    return messages[0]

Getting big chunks is definitely more efficient simply because you are doing less operations. 仅仅因为您执行较少的操作,获取大块数据绝对会更有效。 Think about it like this: if you have a message of length 1000, then your loop will fire 1000 times in first case and in second only 16. Performance loss is obvious. 可以这样考虑:如果您有一条长度为1000的消息,则循环在第一种情况下将触发1000次,而在第二种情况下将仅触发16次。性能损失是显而易见的。 Of course this is because of Python, but even on OS level it is more efficient because of the same reason: making 1000 sys calls instad of 16. And scanning string (by the way: you should scan chunk instead of message ) of length 64 is less expensive then 64 calls to recv . 当然,这是因为Python,但由于相同的原因,即使在OS级别,它也效率更高:进行1000次sys instad的调用为16。并且扫描长度为64的字符串(顺便说一句:您应该扫描chunk而不是message )比64个调用recv便宜。 That's pretty much it. 就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM