select() python windows 中的文件描述符过多

too many file descriptors in select() python in windows

我正在尝试接收大约 1000 个连接到我的服务器,但它不能接收超过 512 个。我可以做些什么来增加打开的连接数量?我是运行windows8.1

不是:我对这些东西很陌生,谢谢你的帮助

这是我的代码;

import asyncore
import socket
import uuid
import time
import threading

class statistics(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        while True:
            entry = raw_input("")
            zaman = int(time.time())
            cmd = receivedCmd
            print "calculating.."
            time.sleep(1)
            if entry == 'istatistik':
                print str(receivedCmd-cmd) + " command/second"
                print "total received commands: " + str(receivedCmd)
                entry = ""
class tcpClient:
    def __init__(self):
        self.clientid = uuid.uuid1(int(time.time()))
        self.buffer = ""
        self.buffer_size = 0
        self.conn_time = time.time()
        self.overflow = 0
        #print str(self.clientid) + " assingned"
    def recv_msg(self, msg):
        global receivedCmd
        self.buffer = msg
        self.buffer_size = len(self.buffer)
        receivedCmd = receivedCmd + 1
        if self.buffer_size >= 1024:
            self.overflow = 1

    def __del__(self):
        print str(self.clientid) + " has left."


class TCPHandler(asyncore.dispatcher_with_send):
    global clist
    def handle_read(self):
        data = self.recv(1024)
        if data:
            if clist[self].overflow:
                self.send("overflow")
                self.handle_close()
            else:
                self.send(data)
                clist[self].recv_msg(data)
    def handle_close(self):
        del clist[self]
        self.close()

    def handle_error(self):
        del clist[self]
        self.close()

class TCPServer(asyncore.dispatcher):
    global clist
    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind((host, port))
        self.listen(5)

    def handle_accept(self):
        self.clist = clist
        pair = self.accept()
        if pair is None:
            pass
        else:
            sock, addr = pair
            #print 'Connection : %s' % repr(addr)
            clist[TCPHandler(sock)] = tcpClient()

if __name__ == '__main__':
    clist = {}
    receivedCmd = 0
    server = TCPServer('', 5000)
    server2 = TCPServer('',5001)

    StaticsThread = statistics()
    StaticsThread.start()
    asyncore.loop()

注意:我仍然无法收到超过 512 个与 Twisted Framework 的连接,我不知道该怎么做。必须有数以千计的连接客户端。请帮忙。

asyncore模块依赖selectOS函数,只支持有限数量的文件描述符。

作为替代方案,使用多线程服务器(我不推荐这个),或者更好的是 Twisted framework,它是事件驱动的(强烈推荐!)。

希望对您有所帮助!


由于 Twisted 在 Windows 下的默认反应器也是基于 select 的,因此您应该考虑改用 IOCP 反应器。

from twisted.internet import iocpreactor
iocpreactor.install()

from twisted.internet import reactor

但也要考虑到 Twisted 更喜欢 Linux 系统(默认反应器是基于 epoll 的)而不是 Windows。也许切换到 Linux 是更好的选择。