- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
要从 python 中的套接字读取数据,请调用 socket.recv
,它具有以下签名:
socket.recv(bufsize[, flags])
python docs for socket.recv含糊地说:
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 设置为非二次方的实际影响是什么?
我见过 many other recommendations使其读取 2 的幂。我也很清楚将数组长度作为 2 的幂通常有用的原因(对长度的位移/屏蔽操作、最佳 FFT 数组大小等),但这些是应用依赖。我只是没有看到 socket.recv
的一般原因。当然不是 specific recommendation在 python 文档中。我也没有在 underlying python code 中看到任何二次幂优化。使其成为特定于 python 的推荐
例如...如果您有一个确切知道传入数据包长度的协议(protocol),显然最好只读取“最多”您正在处理的数据包所需的内容,否则您可能会吃掉下一个数据包,那会很烦人。如果我当前正在处理的数据包只有 42 个字节待处理,我只会将 bufsize 设置为 42。
我错过了什么?当我必须选择任意缓冲区/数组大小时,我通常(总是?)将长度设为 2 的幂,以防万一。这只是多年养成的习惯。 python 文档也只是习惯的受害者吗?
这不是 python 独有的,但由于我专门引用了 python 文档,所以我会这样标记它。
更新:我刚刚检查了系统内核级别的缓冲区大小(或者至少我认为我做了...我做了 cat/proc/sys/net/core/rmem_default
),它是 124928。不是 2 的幂。 rmem_max
是 131071,显然也不是 2 的幂。
在进一步研究这一点时,我真的看不出两个推荐的力量有什么好处。我准备把它称为虚假推荐......
我还添加了 tcp
和 C
标签,因为它们也很相关。
最佳答案
我很确定“2 的幂”建议是基于编辑错误,不应被视为要求。
那条具体的建议是added to the Python 2.5 documentation (和 backported to Python 2.4.3 docs ),响应 Python issue #756104 .报告者为 socket.recv()
使用了不合理的大缓冲区,导致更新。
是 Tim Peters 提出了“2 的力量”概念:
I expect you're the only person in history to try passing such a large value to recv() -- even if it worked, you'd almost certainly run out of memory trying to allocate buffer space for 1.9GB. sockets are a low-level facility, and it's common to pass a relatively small power of 2 (for best match with hardware and network realities).
(我的粗体强调)。我和 Tim 一起工作过,他在网络编程和硬件方面拥有丰富的经验,所以一般来说,当我发表这样的评论时,我会相信他的话。他特别“喜欢”Windows 95 堆栈,他称其为煤矿中的金丝雀,因为它能够在压力下失败。但请注意,他说这是常见的,而不是必须使用 2 的幂。
正是这种措辞导致了文档更新:
This is a documentation bug; something the user should be "warned" about.
This caught me once, and two different persons asked about this in #python, so maybe we should put something like the following in the recv() docs.
"""
For best match with hardware and network realities, the
value of "buffer" should be a relatively small power of 2,
for example, 4096.
"""If you think the wording is right, just assign the bug to me, I'll take care of it.
这里没有人挑战“2 的幂”断言,但编辑从 it is common 移动到 should be 在几个回复的空间中。 p>
对我来说,那些提议更新文档的人更关心的是确保您使用一个小缓冲区,而不是它是否是 2 的幂。这并不是说它不好建议 但是;任何与内核交互的低级缓冲区都受益于与内核数据结构的对齐。
但是,虽然很可能存在一个深奥的堆栈,其中大小为 2 的幂的缓冲区更为重要,但我怀疑 Tim Peters 是否曾为他的经验所指(这是常见的做法)以这种铁定的方式类型转换。如果不同的缓冲区大小对您的特定用例更有意义,请忽略它。
关于python - 使用不是 2 的幂的 bufsize 调用 socket.recv 的实际影响是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6363523/
这是一个非常笼统的问题,我希望我能答对。 我正在研究 SSL/TLS 重新协商并已阅读了一些内容。这是我从阅读中了解到的内容: 从 SSL/TLS 重新协商的角度来看,客户端分为两个主要组,打补丁的和
第一个屏幕是艺术的细节。当我向上滚动时,标题将是 alpha。我点击另一个“艺术”到另一个细节 UI,然后按回到 Previous UI。之前的UI标题是黑色的,怎么变透明了。 布局:
想知道 mv 对基表的影响。它会减慢基表的速度吗?它什么时候开始写入 mv,就像同时写入基表和 mv 一样? 如果我有 local_quorum 的 CL 且 RF=3,客户端是否必须等到写入 mv
似乎在任何地方都找不到太多关于此问题的帮助,所以我想我会在这里尝试。 我正在尝试制作一个简单的 for 循环,当我将鼠标悬停在 html 卡上时,它会隐藏卡中的一些文本。该卡有一个简单的名字和姓氏,我
我有一个程序每帧运行 tick() 方法。我希望一个对象根据设定的重力常数下落,因此我创建了一个 Ball 对象,该对象会将其位置更新为前一帧的位置减去 y 速度。每个刻度 y 速度都会减少重力常数。
我的 KeyHandler 在这里: private void KeyHandler(java.awt.event.KeyEvent evt) {
我有一个方法,其中使用了很多其他类,包括链接列表、队列和堆栈。在我的方法中,我有一个 for 循环,我想在其中弹出堆栈(方便地命名为 s)并将队列(方便地命名为 q)出队到 s1 和 q1。由于某种原
我有一个 JTree 节点数组和另一个自定义对象的相应数组。 我想要什么:当选择 JTree 的节点时,相应对象(其数组中索引与节点数组中所选节点索引相同的对象)的字段填充 JLabels。 我被困在
我知道浏览器完成了处理客户端脚本(Javascript、JQuery 等)的所有工作,但想知道在性能方面是否还有其他重要因素(网络速度、客户端计算机速度、服务器环境) 如果它完全依赖于浏览器(类型和版
我有一个 Android 服务在后台运行,它将使用以下代码: while(true) { ServerSocket server = new ServerSocket(1234); Socke
对JQM有以下疑惑: 1.如果我们在单独的 html 文件中使用重复的 id,对 jquery mobile 有什么影响。 假设我们在单独的 html 文件中有重复的 id,但如果我们不使用该 id
我正在尝试更新两个(inventory、sold)MySQL 表的表库存。 假设我们正在处理的 sku 是 BT888-16 UPDATE inventory JOIN sold ON invento
我使用这种方法来更改我的表格单元格值, 它在 jtable 上改变但在文本文件上没有改变! public class user_AllBooks extends AbstractTableModel
我想在向表中插入数据时创建一个 MYSQL 存储过程,数据也会被插入到其他服务器表中。 我知道这在 ORACLE 数据库中是可能的,但我不知道它是否适用于 MYSQL。 有什么办法吗? 最佳答案 是的
我在 css 方面非常糟糕,只能靠 SO 答案来解决 - 但是我找不到针对这个特定问题的任何解释。 我有一个表单,其中包含一个 textarea 和一个 button(input/submit),仅此
我在一个元素上有动画,但它的移动也会影响 sibling 。如何在不影响兄弟元素的情况下仅在元素上使用动画? 问题示例: function animateSearch() { $('.glyph
我试图在我的 ViewController 中的 UIView 的所有四个边上建立一个阴影 — 在我通过 Xcode 向 UIView 添加约束之前,它工作得很好。我怎样才能使 UIView 的阴影显
自从我使用 JavaScript 以来已经有一段时间了 - 在获得证书之后我开始学习 Perl 并从那时起就一直使用它。我只是想重新开始使用 JS,我已经写了这个,我想说的是,这是一个简单的小脚本,可
我正在处理一个 HTML 元素,我添加了一个复选框,选中后会高亮显示所有文本输入字段。唯一的问题是一些输入字段在表格内,出于某种原因我无法用我的代码影响它们。任何帮助将不胜感激。 相关代码: HTML
我为 String 类创建了一个小扩展,以便方便地从中删除字符。这是它的样子: mutating func drop(characters chars: [String]) { for c i
我是一名优秀的程序员,十分优秀!