thread.error:无法启动新线程

时间:2017-04-22 14:14:31

标签: python multithreading

我的服务器有32个基于Centos6的CPU和32G内存。我已经编写了一个基于twisted的TCP服务器。现在我使用python代码测试它:

# -*- 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个连接线程?测试代码有什么问题?有人可以帮助我吗?先谢谢〜

1 个答案:

答案 0 :(得分:0)

您可能受/proc/sys/kernel/threads-max值的限制。更多信息here

在任何情况下,您都不希望只有1000个线程来监视TCP连接。使用async io。