线程池
code
import os import time from concurrent.futures.thread import ThreadPoolExecutor def test(n): print(f'n:{n}-os.getpid:{os.getpid()}') time.sleep(2) return 'hello' # 定义一个回调函数,异步提交完后,有结果自动调用该函数 def call_back(n): print(f'nn;{n}') print(f'callback:n.result:{n.result()}') if __name__ == '__main__': pool = ThreadPoolExecutor(5) print('zhu') t_list = [] for i in range(10): res = pool.submit(test, i) # res = pool.submit(test, i).add_done_callback(call_back) t_list.append(res) print(f't_list:{t_list}') pool.shutdown() # 等待线程池中所有任务运行完毕,关闭线程池