python 线程池实用总结
线程池的两张方法 submit 和map
from concurrent.futures import ThreadPoolExecutor import time # def sayhello(a): time.sleep(2) return "hello: "+a def main(): seed = ["a","b","c"] # 不使用线程 start1 = time.time() for each in seed: t1 = sayhello(each) print(t1) end1 = time.time() print("time1: "+str(end1-start1)) print('------------------submit----------------------') # 线程池submit用法 # 1.先把值存放在列表中 # 2.在遍历列表取返回值 # 3.将遍历获取的结果存放在列表中 start2 = time.time() lst = [] result_lst = [] with ThreadPoolExecutor(3) as executor: for each in seed: t2 = executor.submit(sayhello, each) lst.append(t2) for i in lst: print(i.result()) result_lst.append(i.result()) print(result_lst) end2 = time.time() print("time2: "+str(end2-start2)) print('---------------map----------------------') # 线程池map的用法 # map的参数:迭代器 # map的返回值是:生成器 # 1、获取生成器(或直接使用list强制装换) # 2、遍历取值 # 3、将遍历的结果放入新的列表 map_lst = [] start3 = time.time() with ThreadPoolExecutor(3) as executor1: t3 = executor1.map(sayhello, seed) for t in t3: map_lst.append(t) print(map_lst) end3 = time.time() print("time3: "+str(end3-start3)) if __name__ == '__main__': main()
other
from concurrent.futures import ThreadPoolExecutor import time # def sayhello(a): time.sleep(2) foo = [1, a] return foo def main(): seed = ["a","b","c"] # 不使用线程 start1 = time.time() for each in seed: t1 = sayhello(each) print(t1) end1 = time.time() print("time1: "+str(end1-start1)) print('---------------map----------------------') # 线程池map的用法 # map 的结果是迭代器,使用for循环取值 map_lst = [] start3 = time.time() with ThreadPoolExecutor(3) as executor1: t3 = executor1.map(sayhello, seed) for t in t3: map_lst.extend(t) print(map_lst) end3 = time.time() print("time3: "+str(end3-start3)) if __name__ == '__main__': main()
区别
map: 1、提交的任务的函数是一样的 2、参数:只需要提交一次目标函数,目标函数的参数放在一个迭代器(列表,字典) submit: 1、提交的任务函数是不一样的,或者执行的过程之可能出现异常(使用map执行过程中发现问题会直接抛出错误) 2、参数:submit每次都需要提交一个目标函数和对应的参数 结果: map可以保证输出的顺序, submit输出的顺序是乱的