Create a thread pool to use with the way, after the task is completed, it will automatically close the resource, otherwise you will need to manually close the thread pool resources
import threading, time from concurrent.futures import ThreadPoolExecutor, as_completed class MyTask(threading.Thread): """ Using python threading method """ def __init__(self, thread_id): threading.Thread.__init__(self) self.thread_id = thread_id DEF RUN (Self): the while . 1 : Print ( ' % S running thread ........ ' % self.thread_id) time.sleep(1) class MyThreadPool(object): """ There are two ways python thread pool """ DEF the __init__ (Self): "" " the init method, the test data structure " "" self.param_data = [] for I in Range (1000 ): self.param_data.append(str(i)) DEF target_task (Self, param): "" " objective method " "" Print (. threading.currentThread () name) # print the current thread's name # Print (param) return param + ' --------- -task ' DEF execute_thread_pool_method1 (Self): "" " The first embodiment of the thread pool '' ' ' '' to create a thread pool, which has threads 10 '' ' with the ThreadPoolExecutor (max_workers = 10, thread_name_prefix = ' Test ' ) AS TPE: # use with, the task will automatically turn off the result = [] # cache-threaded task execution results for i in self.param_data: R & lt = tpe.submit (self.target_task, I) # Submit submit the task, the order of execution is arbitrary result.append (r) for r in as_completed(result): print(r.result()) tpe.shutdown() DEF execute_thread_pool_method2 (Self): "" " Use the thread pool second way ." "" '' ' create a thread pool, which has 10 threads ' '' with ThreadPoolExecutor ( 10 ) AS TPE: # The data type is List result = tpe.map (self.target_task, self.param_data) for R & lt in result: # result set processing Print (R & lt) if __name__ == '__main__': my_task = MyTask(1) my_task.setDaemon (True) # The my_task the threads to the daemon threads my_task.start () Print (threading.active_count ()) # number of threads alive Print # my_thread_pool = MyThreadPool() # my_thread_pool.execute_thread_pool_method1() # my_thread_pool.execute_thread_pool_method2()