我有以下 bash 函数来添加静态 IP
function add_ip_addr() {
for i in {100..200}
do
ipaddr="100.100.100."$i
command="ip addr add "$ipaddr" dev eth0"
echo $command
$($command)
done
}
然后我有以下 Python3-application 来创建 1M 套接字连接
import socket
n = 0
sockSet = set()
try :
for I in range(100, 200):
ipaddr = "100.100.100." + str(I)
print("\rProcessing", ipaddr, end="")
for port in range(10000, 20000):
s1=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.bind((ipaddr, port))
sockSet.add(s1)
n+=1
except OSError as ex:
# Too many open files in system
if ex.errno == 23:
print("\n"+str(n))
但我总是得到Too many open files in system
以下输出的异常
root@c8c1c6a106c2:~/programs/python# python3 socket-limit.py
Processing 100.100.100.163
630730
root@c8c1c6a106c2:~/programs/python#
ulimit 值如下所示
root@c8c1c6a106c2:~/programs/python# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 24682
max locked memory (kbytes, -l) 82000
max memory size (kbytes, -m) unlimited
open files (-n) 1048576
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
root@c8c1c6a106c2:~/programs/python#
/etc/sysctl.conf
数值如下所示
root@c8c1c6a106c2:~/programs/python# tail -6 /etc/sysctl.conf
fs.file-max = 1048576
fs.nr_open = 1048576
net.ipv4.netfilter.ip_conntrack_max = 1048576
net.nf_conntrack_max = 1048576
root@c8c1c6a106c2:~/programs/python#
要实现 100 万个套接字连接,我还需要做哪些其他更改?