简体   繁体   English

Python中的最大TCP连接数?

[英]Maximum number of TCP connections in Python?

I am working in Python on localhost.我在本地主机上使用 Python 工作。 I wrote a raw server just read TCP socket, say in port 50001.我写了一个原始服务器,只是读取 TCP 套接字,比如在端口 50001 中。

Then I tried max client connections:然后我尝试了最大客户端连接数:

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

but after about 3000 connections, exception occurs:但是在大约 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 [Errno 10055] 无法对套接字执行操作,因为系统缺少足够的缓冲区空间或队列已满

So how can I resolve it and max the connection?那么我该如何解决它并最大化连接?

Check the ulimit and quota for the box and the user running the script.检查框的ulimitquota以及运行脚本的用户。 /etc/security/limits.conf may also contain resource restrictions that you might want to modify. /etc/security/limits.conf也可能包含您可能想要修改的资源限制。

Try running ulimit -s unlimited .尝试运行ulimit -s unlimited

Also, ulimit -n will show the max number of open file descriptors/sockets allowed.此外, ulimit -n将显示允许的最大打开文件描述符/套接字数。 That may need modification as well.这也可能需要修改。


Some man pages and reference links:一些手册页和参考链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM