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

时间:2015-07-09 14:54:39

标签: python asyncore

我正在尝试接收大约1000个连接到我的服务器,但它不能接收超过512.我该怎么做才能增加打开连接的数量?我正在运行Windows 8.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()

注意:我仍然无法使用Twisted Framework获得超过512个连接,我不知道该怎么做。必须有成千上万的连接客户端。请帮忙。

1 个答案:

答案 0 :(得分:0)

asyncore模块依赖于select操作系统功能,该功能仅支持有限数量的文件描述符。

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

希望这有帮助!

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

from twisted.internet import iocpreactor
iocpreactor.install()

from twisted.internet import reactor

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