macos 雪豹上的 Python,如何打开 >255 个套接字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1364955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python on Snow Leopard, how to open >255 sockets?
提问by Tader
Consider this code:
考虑这个代码:
import socket
store = []
scount = 0
while True:
scount+=1
print "Creating socket %d" % (scount)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
store.append(s)
Gives the following result:
给出以下结果:
Creating socket 1
Creating socket 2
...
Creating socket 253
Creating socket 254
Traceback (most recent call last):
File "test_sockets.py", line 9, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/socket.py", line 159, in __init__
socket.error: (24, 'Too many open files')
Checking sysctl for the allowed number of open files gives:
检查 sysctl 以获取允许的打开文件数量:
$ sysctl -A |grep maxfiles
kern.maxfiles = 12288
kern.maxfilesperproc = 10240
kern.maxfiles: 12288
kern.maxfilesperproc: 10240
Which is way more than the 253 sockets I could successfully open...
这比我可以成功打开的 253 个套接字还多...
Could someone please help me in getting this number up to over 500? I am trying to simulate a peer to peer network using real sockets (requirement), with only 50 simulated nodes and 5 outgoing and 5 incoming connections each, would give the number of 500 needed sockets.
有人可以帮我把这个数字增加到 500 多吗?我正在尝试使用真实套接字(要求)模拟点对点网络,只有 50 个模拟节点和 5 个传出连接和 5 个传入连接,每个连接将提供 500 个所需的套接字。
By the way, running this same code under Linux gives me about 1020 sockets, which is more the way I like it.
顺便说一句,在 Linux 下运行相同的代码给了我大约 1020 个套接字,这更像是我喜欢的方式。
回答by ACoolie
You can increase available sockets with ulimit
. Looks like 1200 is the max for non-root users in bash. I can get up to 10240 with zsh.
您可以使用 来增加可用的套接字ulimit
。看起来 1200 是 bash 中非 root 用户的最大值。我可以用 zsh 达到 10240。
$ ulimit -n 1200
$ python sockets
....
Creating socket 1197
Creating socket 1198
Traceback (most recent call last):
File "sockets", line 7, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 182, in __init__
socket.error: [Errno 24] Too many open files
回答by Chris Bunch
Did you install XCode and the developer tools off the Snow Leopard install disk? I'm able to open way more ports than you're able to:
您是否从 Snow Leopard 安装盘安装了 XCode 和开发人员工具?我能够打开比你更多的端口:
Creating socket 1
Creating socket 2
...
Creating socket 7161
Creating socket 7162
Creating socket 7163
Creating socket 7164
Creating socket 7165
Creating socket 7166
Traceback (most recent call last):
File "socket-test.py", line 7, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/socket.py", line 159, in __init__
socket.error: (24, 'Too many open files')
sysctl
shows me a lot more info then your output shows (even with the grep) but the four lines you have match mine exactly, so all I can think of is needing something from the dev tools on the disk.
sysctl
向我展示了更多的信息,然后您的输出显示(即使使用 grep),但是您拥有的四行与我的完全匹配,所以我能想到的就是需要磁盘上的开发工具中的某些内容。