什么是调用socket.recv与BUFSIZE不是2的幂的实际影响? [英] What is the actual impact of calling socket.recv with a bufsize that is not a power of 2?

查看:154
本文介绍了什么是调用socket.recv与BUFSIZE不是2的幂的实际影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在蟒蛇套接字读取数据时,你叫 socket.recv ,其中有这样的签名:

To read data from a socket in python, you call socket.recv, which has this signature:

socket.recv(BUFSIZE [,标志])

借助 python文档为socket.recv 隐约状态:

注意:对于硬件和网络的现实最佳匹配,价值
  BUFSIZE应该是一个相对小的
  2的幂,例如,4096。

Note: For best match with hardware and network realities, the value of bufsize should be a relatively small power of 2, for example, 4096.

:什么是的硬件和网络的现实最佳匹配的是什么意思?什么是的实际的设置BUFSIZE到影响非电源的二?

Question: What does "best match with hardware and network realities" mean? What is the actual impact of setting bufsize to a non-power-of-two?

我见过的<一个href=\"http://hackage.haskell.org/packages/archive/network-enumerator/0.1.2/doc/html/Network-Socket-Enumerator.html\">many <一href=\"http://stackoverflow.com/questions/2862071/how-large-should-my-recv-buffer-be-when-calling-recv-in-the-socket-library/2862096#2862096\">other <一href=\"http://stackoverflow.com/questions/822001/python-sockets-buffering/822788#822788\">recommendations使该读出的2的乘方,我也深知原因,当它是经常有益的阵列的长度为两个(bitshift /上的长度屏蔽操作,最佳FFT数组大小等)的能力,但这些都是取决于应用。我只是我没有看到它的原因一般与 socket.recv 。当然不是给具体建议点。我还没有看到电源的两任何优化在蟒蛇$底层C $ç使它成为一个特定的python-推荐

I've seen many other recommendations to make this read a power of 2. I'm also well aware of reasons when it is often useful to have array lengths as powers of two (bitshift/masking operations on the length, optimal FFT array size, etc), but these are application dependent. I just am not seeing the general reason for it with socket.recv. Certainly not to the point of the specific recommendation in the python documentation. I also don't see any power-of-two optimizations in the underlying python code to make it a python-specific recommendation

例如...如果你有其中传入数据包长度正好是已知的协议,这显然是preferrable为只读在最需要的是什么,你正在处理的数据包,否则你可能会可能吃进下一个数据包,这将是刺激性的。如果我当前处理的数据包只有42字节之前,我只打算BUFSIZE设置为42。

For example... if you have a protocol where the incoming packet length is exactly known, it is obviously preferrable to only read "at most" what is needed for the packet you are dealing with, otherwise you could potentially eat into the next packet and that would be irritating. If the packet I'm currently processing only has 42 bytes pending, I'm only going to set bufsize to 42.

我在想什么?如果我必须选择一个任意缓冲区/数组大小我平时(总是?)使长度的两个电源,以防万一。这仅仅是发展了很多年的习惯。是Python文档还只是一种习惯的受害者?

What am I missing? When I have to choose an arbitrary buffer/array size I usually (always?) make the length a power of two, just in case. This is just a habit developed over many years. Are the python docs also just a victim of habit?

这是不是排他性的蟒蛇,但因为我专门引用python文档我将其标记为此类。

This isn't exclusive to python, but since I'm specifically referencing the python docs I'll tag it as such.

更新:我只是检查在内核级别的缓冲区的大小我的系统上(或者至少我觉得我做......我做了执行cat / proc / SYS /网/核心/ rmem_default ),这是124928.不是2的力量。 rmem_max 是131071,也显然不是二的幂。

UPDATE: I just checked the size of the buffer at the kernel level on my system (or at least I think I did... I did cat /proc/sys/net/core/rmem_default) and it was 124928. Not a power of two. rmem_max was 131071, also clearly not a power of two.

在寻找到这种更我实在看不出有两种建议(S)的功率任何好处呢。我正要准备调用它作为一个假的建议...

In looking into this more I really cannot see any benefit in the power of two recommendation(s) yet. I'm about ready to call it as a bogus recommendation...

我还添加了 TCP C 标签,因为他们也有关联。

I also added tcp and C tags since they are also relevant.

推荐答案

看一看在这问题。它()过处理与的recv和答案可能更完整的了解是有益的。

Have a look at the answers on this SO question. It deals with recv() too and the answers may be helpfull for a more complete understanding.

这不会回答这个问题,但会提供更多的见识。

It won't answer the question but will provide a bit more insight.

这篇关于什么是调用socket.recv与BUFSIZE不是2的幂的实际影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆