我在本地主机上使用 Python 工作。我写了一个原始服务器只读取 TCP 套接字,比如在端口 50001 中。
然后我尝试了最大客户端连接数:
def rawMultiConn(threadnum = 10000):
g_event = threading.Event()
def threadfn():
sockets = [socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for i in range(threadnum)]
for s in sockets:
s.connect(('localhost', SERVER_PORT))
g_event.wait()
for s in sockets: s.close()
t = threading.Thread(target = threadfn)
t.start()
g_event.set()
t.join()
但是大约3000次连接后,出现异常:
[Errno 10055] An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full
那么我该如何解决它并最大化连接呢?
最佳答案
检查盒子和运行脚本的用户的 ulimit
和 quota
。 /etc/security/limits.conf
也可能包含您可能想要修改的资源限制。
尝试运行 ulimit -s unlimited
。
此外,ulimit -n
将显示允许打开的文件描述符/套接字的最大数量。这可能也需要修改。
一些手册页和引用链接:
关于python - Python 中的最大 TCP 连接数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7748898/