0
我的服務器有32個CPU和基於Centos6.I 32G內存基於twisted.Now我用這樣的Python代碼測試它寫了一個TCP服務器:thread.error:無法啓動新的線程
# -*- coding: UTF-8 -*-.
import socket, optparse, time, os, threading
from sensor import Sensor
def strWrapper(sock, data):
host, port = sock.getsockname() # local IP_addr
str = '%s:%s,%d,%s,%s\r\n' % (host, port, data[0], data[1], data[2])
return str
def main(threadID, args):
options, address = args
host, port = address # remote addr
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(address)
print "%d.Connected with %s:%s" % (threadID, host, port)
sensor = Sensor(options.typeid)
for j in xrange(0, 5000):
#sensor.get_data() generate some random number
req = strWrapper(s, sensor.get_data())
s.send(req)
s.close()
if __name__ == '__main__':
start = time.time()
args = parse_args()
threads = []
for i in xrange(0, 10000):
thread = threading.Thread(target=main, args=(i, args))
threads.append(thread)
for thr in threads:
thr.start()
# thr.join()
# end = time.time()
# print 'Task runs %0.2f seconds.' % ((end - start))
然而,當有684個線程客戶端得到一個錯誤:
...
680.Connected with 10.10.102.9:10010
681.Connected with 10.10.102.9:10010
682.Connected with 10.10.102.9:10010
683.Connected with 10.10.102.9:10010
684.Connected with 10.10.102.9:10010
Traceback (most recent call last):
File "client/tcpclient.py", line 85, in <module>
thr.start()
File "/usr/local/lib/python2.7/threading.py", line 745, in start
_start_new_thread(self.__bootstrap,())
thread.error: can't start new thread
怎麼可能我會在租賃10000連接線沒有錯誤?測試代碼有什麼問題?任何人都可以幫助我?提前致謝〜
我運行'cat/proc/sys/kernel/threads-max'並得到'513033'。 –