2015-03-02 94 views
-2

我需要知道爲什麼當我運行下面的代碼它給我這個錯誤蟒蛇: - 無法啓動新的線程

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

你試圖開始900線程,並可能在操作系統上達到極限。我不知道你要做什麼,但我會考慮Twisted,這可能會有更多的選項來運行大容量的TCP服務器。

+0

謝謝丹,我需要編寫一個程序來監聽所有端口(65535) – 2015-03-03 12:44:42

+0

通常,1024和更低的端口是保留的。我質疑你正在嘗試解決的任務需要多個開放端口......但Twisted應該有所幫助,因爲你可以綁定到多個端口而不使用那麼多的進程。 – 2015-03-03 19:24:32