Python在Snow Leopard上,如何打开> 255套接字? [英] Python on Snow Leopard, how to open >255 sockets?

查看:103
本文介绍了Python在Snow Leopard上,如何打开> 255套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

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)

给出以下结果:

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')

在sysctl中检查允许的打开文件数会得出:

Checking sysctl for the allowed number of open files gives:

$ sysctl -A |grep maxfiles
kern.maxfiles = 12288
kern.maxfilesperproc = 10240
kern.maxfiles: 12288
kern.maxfilesperproc: 10240

除了我可以成功打开的253个插座之外,还有其他更多方法...

Which is way more than the 253 sockets I could successfully open...

有人可以帮我把这个数字提高到500以上吗?我正在尝试使用实际套接字(要求)模拟对等网络,该套接字只有50个模拟节点,每个节点有5个传出连接和5个传入连接,将提供500个所需的套接字数量.

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.

顺便说一句,在Linux下运行相同的代码会给我大约1020个套接字,这是我喜欢的方式.

By the way, running this same code under Linux gives me about 1020 sockets, which is more the way I like it.

推荐答案

您可以使用ulimit增加可用的套接字.看起来1200是bash中非root用户的最大值.我可以用zsh达到10240.

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 -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

这篇关于Python在Snow Leopard上,如何打开&gt; 255套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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