python-3.x - 无法在 Ubuntu 中创建百万套接字连接
问题描述
我有以下 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 万个套接字连接,我还需要做哪些其他更改?
解决方案
推荐阅读
- css - Angular Material:如何在 TS 中设置具有不同背景颜色的每个 mat-horizontal-stepper 步进器图标
- javascript - 在javascript中循环对象,为什么调用什么“键”无关紧要?
- c - 程序中的循环“<”与“<=”打印格式问题
- python - 带解码器的类似哈希的字符串缩短器
- opencv - OpenCV:灰度图像的图像分割
- r - 根据来自另一个数据帧的两个条件子集一个数据帧
- flutter - 尝试在 VS Code/Flutter 上运行时出现调试错误
- python - 如何从特定列中获取数据并将其分开?
- python - Python Pandas 打印列标题时出错
- mysql - 插入前创建触发器