1

My servers have 32 CPU and 32G memory based on Centos6.I have written a TCP server based on twisted.Now I test it using python code like this:

# -*- 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))

However, when there has 684 threads client get a error:

...
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

How could I get at lease 10000 connection threads without error? What's wrong with the test code?Can anyone help me?Thanks in advance~

Joe Zhow
  • 833
  • 7
  • 17

1 Answers1

0

You are probably limited by the value of /proc/sys/kernel/threads-max. More information here.

In any case, you do not want to have 1000 threads just to monitor tcp connections. Use async io.

user2722968
  • 13,636
  • 2
  • 46
  • 67