• twisted连接数问题


    原文地址:http://ciniao.me/article.php?id=10


    =========================================================


    twisted默认用的是select()模式,而Windows的对文件描述符(file descriptor)有一定限制,这个限制值是512,在Linux的下这个限制为1024, 如果超过这个限制值,就会出现上面的异常。如果要在windows中有更好的表 现,看来得用iocp,而linux下,用epoll则是更合适的方案,而Twisted自身就已经支持了这2种模式,看看如何启用:
    windows:

    1. from twisted.internet import iocpreactor

    2. iocpreactor.install()


    linux:

    1. from twisted.internet import epollreactor

    2. epollreactor.install()


        我的程序是在windows上开发的,最终部署到linux上,所以得写一个简单的判断来根据系统选择对应的模式,完整的服务端代码调整为:

    1. import os

    2. if os.name!='nt':

    3.     from twisted.internet import epollreactor

    4.     epollreactor.install()    

    5. else:

    6.     from twisted.internet import iocpreactor

    7.     iocpreactor.install()

    8. from twisted.internet.protocol import Factory,Protocol

    9. from twisted.internet import reactor


    10. class gameSocket(Protocol):

    11.     #有新用户连接至服务器

    12.     def connectionMade(self):

    13.         print 'New Client'

    14.     

    15.     #客户端断开连接

    16.     def connectionLost(self,reason):

    17.         print 'Lost Client'

    18.     

    19.     #收到客户端发送数据

    20.     def dataReceived(self, data):

    21.         print 'Get data:' + str(data)

    22.         #向该客户端发送数据

    23.         self.transport.write('bingo!i got your msg:'+ str(data))


    24. if __name__=='__main__':

    25.     f = Factory()

    26.     f.protocol = gameSocket

    27.     reactor.listenTCP(5200,f)

    28.     print 'server started...'

    29.     reactor.run()


  • 相关阅读:
    Lamp环境搭建
    jquery之下拉列表select
    jquery之radio
    php连接postgresql
    ipython的notebook
    python连接postgresql数据库
    django最简单表单入门
    css制作简单下拉菜单
    下拉列表简单操作
    css制作最简单导航栏
  • 原文地址:https://www.cnblogs.com/cly84920/p/4426434.html
Copyright © 2020-2023  润新知