Python 多进程练习
1 import multiprocessing 2 import os 3 import time 4 5 6 def get_html(n): 7 time.sleep(n) 8 print('sub_progress success') 9 return n 10 11 12 if __name__ == '__main__': 13 # progress = multiprocessing.Process(target=get_html,args=(2,)) 14 # progress.start() 15 # progress.join() 16 # print('main progress end') 17 18 19 pool = multiprocessing.Pool(multiprocessing.cpu_count()) 20 result = pool.apply_async(get_html, args=(3,)) 21 22 #等待所有任务完成 23 pool.close() 24 pool.join() 25 print(result.get()) 26 import time 27 from concurrent.futures import ThreadPoolExecutor, as_completed 28 from concurrent.futures import ProcessPoolExecutor 29 30 def fib(n): 31 if n<=2: 32 return 1 33 return fib(n-1)+fib(n-2) 34 35 36 with ProcessPoolExecutor(3) as executor: 37 all_task = [executor.submit(fib,num) for num in range(25,35)] 38 start_time = time.time() 39 for future in as_completed(all_task): 40 data = future.result() 41 print('get {} page'.format(data)) 42 43 print('last time is:{}'.format(time.time()-start_time))