Python多线程多进程实例对比解析

时间:2021-05-22

多线程适合于多io操作

多进程适合于耗cpu(计算)的操作

# 多进程编程# 耗cpu的操作,用多进程编程, 对于io操作来说,使用多线程编程import timefrom concurrent.futures import ThreadPoolExecutor, as_completedfrom concurrent.futures import ProcessPoolExecutordef fib(n): if n <= 2: return 1 return fib(n - 2) + fib(n - 1)if __name__ == '__main__': # 1. 对于耗cpu操作,多进程优于多线程 # with ThreadPoolExecutor(3) as executor: # all_task = [executor.submit(fib, num) for num in range(25, 35)] # start_time = time.time() # for future in as_completed(all_task): # data = future.result() # print(data) # print("last time :{}".format(time.time() - start_time)) # 3.905290126800537 # 多进程 ,在window环境 下必须放在main方法中执行,否则抛异常 with ProcessPoolExecutor(3) as executor: all_task = [executor.submit(fib, num) for num in range(25, 35)] start_time = time.time() for future in as_completed(all_task): data = future.result() print(data) print("last time :{}".format(time.time() - start_time)) # 2.6130592823028564

可以看到在耗cpu的应用中,多进程明显优于多线程 2.6130592823028564 < 3.905290126800537

下面模拟一个io操作

# 多进程编程# 耗cpu的操作,用多进程编程, 对于io操作来说,使用多线程编程import timefrom concurrent.futures import ThreadPoolExecutor, as_completedfrom concurrent.futures import ProcessPoolExecutordef io_operation(n): time.sleep(2) return nif __name__ == '__main__': # 1. 对于耗cpu操作,多进程优于多线程 # with ThreadPoolExecutor(3) as executor: # all_task = [executor.submit(io_operation, num) for num in range(25, 35)] # start_time = time.time() # for future in as_completed(all_task): # data = future.result() # print(data) # print("last time :{}".format(time.time() - start_time)) # 8.00358772277832 # 多进程 ,在window环境 下必须放在main方法中执行,否则抛异常 with ProcessPoolExecutor(3) as executor: all_task = [executor.submit(io_operation, num) for num in range(25, 35)] start_time = time.time() for future in as_completed(all_task): data = future.result() print(data) print("last time :{}".format(time.time() - start_time)) # 8.12435245513916

可以看到 8.00358772277832 < 8.12435245513916, 即是多线程比多进程更牛逼!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章