我很好奇我养的是什么限制?我有下一个代码:
import httplib, resource, socket, traceback
print("NOFILE: %s" % str(resource.getrlimit(resource.RLIMIT_NOFILE)))
socket.setdefaulttimeout(100000)
conns = []
for _ in xrange(10000000):
con = httplib.HTTPConnection('ya.ru') # or 93.158.134.3
try:
con.connect()
conns.append(con)
except:
print("Total connections: %s\n" % len(conns))
print(traceback.format_exc())
break
input("Press any key to exit...")
当HTTPConnection
使用主机名时,我会得到下一个输出:
NOFILE: (2560, 9223372036854775807)
Total connections: 1019
Traceback (most recent call last):
File "test.py", line 13, in <module>
con.connect()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 772, in connect
self.timeout, self.source_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 65] No route to host
当我使用直接IP地址时:
NOFILE: (2560, 9223372036854775807)
Total connections: 1021
Traceback (most recent call last):
File "test.py", line 13, in <module>
con.connect()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 772, in connect
self.timeout, self.source_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 36] Operation now in progress
它看起来非常相同,因为1019,1021和1024是非常接近的值。我认为1024在我的操作系统中是一个限制。
uname -a
Darwin hackmachine 13.3.0 Darwin Kernel Version 13.3.0: Tue Jun 3 21:27:35 PDT 2014; root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 2560
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 709
virtual memory (kbytes, -v) unlimited
所以,我的问题是:
UPD:在Debian 7
上尝试了同样的事情。 ulimit -n <limit>
按预期工作,我可以建立ulimit
设置的连接数。因此,它看起来像OS X
特定问题。