Twisted: number of client connections to TCP server limited?

我们两清 提交于 2019-12-05 20:25:39

The number 511 is rather suspicious. It's close enough to a power of two that my initial hunch is an arbitrarily imposed limit or a bug.

Since you mentioned you're on Windows, I think I can say it's an arbitrarily imposed limit with a bit of confidence. The number of sockets supported by select(2) is limited on all platforms, but the limit is even lower than usual on Windows. By default, it's actually 64. However, Python ups this limit to 512 (the limit isn't mutable like that on most platforms, but it is on Windows - at C compile time).

Failing after 511 users sounds like just what would happen if this were the limit on your system - the 512th socket is the one listening for connections.

Most limits like this are hard to find in a general way. Usually you have to dig into what low-level API or system calls are being used and then look up their documentation, or ask and hope someone else who has (unfortunately) memorized all the various limits helps you out. :)

You can avoid this limit by using the IOCP-based reactor on Windows. It's very easy to switch to. Just insert these lines before the first lines in your server:

from twisted.internet import iocpreactor
iocpreactor.install()

Everything else stays the same (and, in particular, your existing reactor import stays the same, and you keep using reactor, you don't switch to using iocpreactor anywhere else in your program).

You can read more about reactor selection in Twisted's online documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!