Python中的最大TCP连接数? [英] Maximum number of TCP connections in Python?
问题描述
我正在localhost上使用Python工作。我写了一个原始服务器,只是读取TCP套接字,例如在端口50001中。
I am working in Python on localhost. I wrote a raw server just read TCP socket, say in port 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()
,但是经过约3000个连接后,发生异常:
but after about 3000 connections, exception occurs:
[Errno 10055]无法对套接字执行操作,因为
系统缺少足够的缓冲区空间或队列
[Errno 10055] An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full
那么我该如何解决它并最大化连接?
So how can I resolve it and max the connection?
推荐答案
检查 ulimit
和 quota
,表示该框和运行脚本的用户。 /etc/security/limits.conf
也可能包含您可能想要修改的资源限制。
Check the ulimit
and quota
for the box and the user running the script. /etc/security/limits.conf
may also contain resource restrictions that you might want to modify.
尝试运行 ulimit -s无限制
。
另外, ulimit -n
将显示允许的打开文件描述符/套接字的最大数量。
Also, ulimit -n
will show the max number of open file descriptors/sockets allowed. That may need modification as well.
一些手册页和参考链接:
- ulimit
- quota
- limits.conf
- lsof
这篇关于Python中的最大TCP连接数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!