最近项目中用到了多进程来算模型的特征,看看python高级库的多进程与多线程的效率
1 from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor 2 from concurrent import futures 3 import time 4 5 6 class Fast(object): 7 8 @staticmethod 9 def get_data(seq): 10 11 time.sleep(0.02) 12 return seq 13 14 15 if __name__ == '__main__': 16 data_list_p = [] 17 data_list_t = [] 18 data_list_ = [] 19 20 # 进程 21 def main_p(): 22 fast_p = Fast() 23 with ProcessPoolExecutor(max_workers=8) as executor: 24 future = [executor.submit(fast_p.get_data, i) for i in range(100)] 25 for i in futures.as_completed(future): 26 data_list_p.append(i.result()) 27 print("进程", data_list_p) 28 start = time.time() 29 main_p() 30 end = time.time() 31 print(end - start, "进程") 32 33 # 线程 34 def main_t(): 35 fast_t = Fast() 36 with ThreadPoolExecutor(max_workers=8) as executor: 37 future = [executor.submit(fast_t.get_data, i) for i in range(100)] 38 futures.as_completed(future) 39 for i in futures.as_completed(future): 40 data_list_t.append(i.result()) 41 print("线程", data_list_t) 42 start_t = time.time() 43 main_t() 44 end_t = time.time() 45 print(end_t - start_t, "线程") 46 47 # 原始 48 start_ = time.time() 49 fast = Fast() 50 [data_list_.append(fast.get_data(i)) for i in range(100)] 51 print(data_list_) 52 53 end_ = time.time() 54 print(end_ - start_)
输出如下:
小结:io频繁推荐多线程,计算频繁推荐多进程