import timefrom threading import current_threadfrom concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutordef f1(n,s): time.sleep(1) # print('%s号子线程'%current_thread().ident) # print(n,s) returnif __name__ == '__main__': tp = ThreadPoolExecutor(4) # tp = ProcessPoolExecutor(4) # tp.map(f1,range(10)) #异步提交任务,参数同样是任务名称,可迭代对象 res_list = [] for i in range(10): res = tp.submit(f1,i,'baobao') #submit是给线程池异步提交任务, print(res) # res.result() res_list.append(res) # for r in res_list: # print(r.result()) tp.shutdown() #主线程等待所有提交给线程池的任务,全部执行完毕 close + join for r in res_list: print(r.result()) print('主线程结束')