启动线程太多,导致抛出异常: cant't start new thread,需要限制start线程数
import threading thread_list = [] pool_sema = threading.BoundedSemaphore(10) def test(): with pool_sema: print('test....') time.sleep(1) if __name__ == '__main__': for i in range(pow(10, 6)): t = threading.Thread(target=test) t.start() thread_list.append(t) print('当前线程数: {0}'.format(threading.active_count())) while threading.active_count() > pow(10, 4): time.sleep(0.001) for i in thread_list: i.join()