python: - 无法启动新线程

时间:2015-03-02 17:36:43

标签: python

我需要知道为什么当我运行以下代码时它会给我这个错误

Traceback (most recent call last):
  File "C:\Users\moksh\Desktop\moksh.py", line 29, in <module>
    server_B_thread.start()
error: can't start new thread

代码:

#!/usr/bin/python

import threading
import time
import SocketServer
import socket 

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        self.allow_reuse_address = True
        self.data = self.request.recv(1024).strip()
        print "%s wrote: " % self.client_address[0]
        print 'Connection from',self.client_address[0]
        print self.data
        self.request.send(self.data.upper())

class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    pass

if __name__ == "__main__":

    HOST = '0.0.0.0'
    PORT = 1000
    while PORT <1900: 
       server_B = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
       server_B_thread = threading.Thread(target=server_B.serve_forever)
       server_B_thread.setDaemon(True)
       server_B_thread.start()
       PORT +=1

    while 1:
        time.sleep(1)

1 个答案:

答案 0 :(得分:1)

您正在尝试启动900个线程,并且可能会对操作系统施加限制。我不知道你要做什么,但我会调查Twisted,这可能有更多选项来运行高容量TCP服务器。