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


Posted in Python onMarch 12, 2020

多线程适合于多io操作

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

# 多进程编程
# 耗cpu的操作,用多进程编程, 对于io操作来说,使用多线程编程
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from concurrent.futures import ProcessPoolExecutor


def 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 time
from concurrent.futures import ThreadPoolExecutor, as_completed
from concurrent.futures import ProcessPoolExecutor

def io_operation(n):
  time.sleep(2)
  return n


if __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, 即是多线程比多进程更牛逼!

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

Python 相关文章推荐
python引用DLL文件的方法
May 11 Python
django 常用orm操作详解
Sep 13 Python
python机器学习理论与实战(一)K近邻法
Jan 28 Python
详解django自定义中间件处理
Nov 21 Python
windows下 兼容Python2和Python3的解决方法
Dec 05 Python
使用python Telnet远程登录执行程序的方法
Jan 26 Python
python3实现指定目录下文件sha256及文件大小统计
Feb 25 Python
Python实现的删除重复文件或图片功能示例【去重】
Apr 23 Python
python图形工具turtle绘制国际象棋棋盘
May 23 Python
python 判断三个数字中的最大值实例代码
Jul 24 Python
Python爬虫实现vip电影下载的示例代码
Apr 20 Python
Python实现爬取网页中动态加载的数据
Aug 17 Python
Python线程协作threading.Condition实现过程解析
Mar 12 #Python
Python 实现网课实时监控自动签到、打卡功能
Mar 12 #Python
Python基于read(size)方法读取超大文件
Mar 12 #Python
Python函数生成器原理及使用详解
Mar 12 #Python
python deque模块简单使用代码实例
Mar 12 #Python
python中安装django模块的方法
Mar 12 #Python
python3 sorted 如何实现自定义排序标准
Mar 12 #Python
You might like
Win2000+Apache+MySql+PHP4+PERL安装使用小结
2006/10/09 PHP
PHP中数组合并的两种方法及区别介绍
2012/09/14 PHP
基于php使用memcache存储session的详解
2013/06/25 PHP
laravel 修改.htaccess文件 重定向public的解决方法
2019/10/12 PHP
JQuery自适应IFrame高度(支持嵌套 兼容IE,ff,safafi,chrome)
2011/03/28 Javascript
javascript html5实现表单验证
2016/03/01 Javascript
AngularJS ng-mousedown 指令
2016/08/02 Javascript
js实现图片切换(动画版)
2016/12/25 Javascript
javascript数据类型详解
2017/02/07 Javascript
js中toString()和String()区别详解
2017/03/23 Javascript
微信小程序block的使用教程
2018/04/01 Javascript
使用VUE+iView+.Net Core上传图片的方法示例
2019/01/04 Javascript
JavaScript实现简单计算器功能
2019/12/19 Javascript
jQuery+PHP+Ajax实现动态数字统计展示功能
2019/12/25 jQuery
微信小程序实现点击页面出现文字
2020/09/21 Javascript
.netcore+vue 实现压缩文件下载功能
2020/09/24 Javascript
python创建只读属性对象的方法(ReadOnlyObject)
2013/02/10 Python
django开发之settings.py中变量的全局引用详解
2017/03/29 Python
Python使用matplotlib填充图形指定区域代码示例
2018/01/16 Python
Python冲顶大会 快来答题!
2018/01/17 Python
对python 读取线的shp文件实例详解
2018/12/22 Python
简单了解Python3里的一些新特性
2019/07/13 Python
Python3实现打印任意宽度的菱形代码
2020/04/12 Python
美国二手复古奢侈品包包购物网站:LXRandCo
2019/06/18 全球购物
全球领先的中国制造商品在线批发平台:DHgate
2020/01/28 全球购物
建筑工程毕业生自我鉴定
2014/01/14 职场文书
家长对老师的感言
2014/03/11 职场文书
股东协议书
2014/04/14 职场文书
感恩母亲节演讲稿
2014/05/07 职场文书
2014年局领导班子自身建设情况汇报
2014/11/21 职场文书
房地产工程部经理岗位职责
2015/04/09 职场文书
售后服务质量承诺书
2015/04/29 职场文书
Go使用协程交替打印字符
2021/04/29 Golang
总结Java对象被序列化的两种方法
2021/06/30 Java/Android
css布局巧妙技巧之css三角示例的运用
2022/03/16 HTML / CSS
mysql 8.0.27 绿色解压版安装教程及配置方法
2022/04/20 MySQL