python3下multiprocessing、threading和gevent性能对比----暨进程池、线程池和协程池性能对比
python3下multiprocessing、threading和gevent性能对比----暨进程池、线程池和协程池性能对比
目前计算机程序一般会遇到两类I/O:硬盘I/O和网络I/O。我就针对网络I/O的场景分析下python3下进程、线程、协程效率的对比。进程采用multiprocessing.Pool进程池,线程是自己封装的进程池,协程采用gevent的库。用python3自带的urlllib.request和开源的requests做对比。代码如下:
- import urllib.request
- import requests
- import time
- import multiprocessing
- import threading
- import queue
- def startTimer():
- return time.time()
- def ticT(startTime):
- useTime = time.time() - startTime
- return round(useTime, 3)
- #def tic(startTime, name):
- # useTime = time.time() - startTime
- # print('[%s] use time: %1.3f' % (name, useTime))
- def download_urllib(url):
- req = urllib.request.Request(url,
- headers={'user-agent': 'Mozilla/5.0'})
- res = urllib.request.urlopen(req)
- data = res.read()
- try:
- data = data.decode('gbk')
- except UnicodeDecodeError:
- data = data.decode('utf8', 'ignore')
- return res.status, data
- def download_requests(url):
- req = requests.get(url,
- headers={'user-agent': 'Mozilla/5.0'})
- return req.status_code, req.text
- class threadPoolManager:
- def __init__(self,urls, workNum=10000,threadNum=20):
- self.workQueue=queue.Queue()
- self.threadPool=[]
- self.__initWorkQueue(urls)
- self.__initThreadPool(threadNum)
- def __initWorkQueue(self,urls):
- for i in urls:
- self.workQueue.put((download_requests,i))
- def __initThreadPool(self,threadNum):
- for i in range(threadNum):
- self.threadPool.append(work(self.workQueue))
- def waitAllComplete(self):
- for i in self.threadPool:
- if i.isAlive():
- i.join()
- class work(threading.Thread):
- def __init__(self,workQueue):
- threading.Thread.__init__(self)
- self.workQueue=workQueue
- self.start()
- def run(self):
- while True:
- if self.workQueue.qsize():
- do,args=self.workQueue.get(block=False)
- do(args)
- self.workQueue.task_done()
- else:
- break
- urls = ['http://www.ustchacker.com'] * 10
- urllibL = []
- requestsL = []
- multiPool = []
- threadPool = []
- N = 20
- PoolNum = 100
- for i in range(N):
- print('start %d try' % i)
- urllibT = startTimer()
- jobs = [download_urllib(url) for url in urls]
- #for status, data in jobs:
- # print(status, data[:10])
- #tic(urllibT, 'urllib.request')
- urllibL.append(ticT(urllibT))
- print('1')
- requestsT = startTimer()
- jobs = [download_requests(url) for url in urls]
- #for status, data in jobs:
- # print(status, data[:10])
- #tic(requestsT, 'requests')
- requestsL.append(ticT(requestsT))
- print('2')
- requestsT = startTimer()
- pool = multiprocessing.Pool(PoolNum)
- data = pool.map(download_requests, urls)
- pool.close()
- pool.join()
- multiPool.append(ticT(requestsT))
- print('3')
- requestsT = startTimer()
- pool = threadPoolManager(urls, threadNum=PoolNum)
- pool.waitAllComplete()
- threadPool.append(ticT(requestsT))
- print('4')
- import matplotlib.pyplot as plt
- x = list(range(1, N+1))
- plt.plot(x, urllibL, label='urllib')
- plt.plot(x, requestsL, label='requests')
- plt.plot(x, multiPool, label='requests MultiPool')
- plt.plot(x, threadPool, label='requests threadPool')
- plt.xlabel('test number')
- plt.ylabel('time(s)')
- plt.legend()
- plt.show()
运行结果如下:
从上图可以看出,python3自带的urllib.request效率还是不如开源的requests,multiprocessing进程池效率明显提升,但还低于自己封装的线程池,有一部分原因是创建、调度进程的开销比创建线程高(测试程序中我把创建的代价也包括在里面)。
下面是gevent的测试代码:
- import urllib.request
- import requests
- import time
- import gevent.pool
- import gevent.monkey
- gevent.monkey.patch_all()
- def startTimer():
- return time.time()
- def ticT(startTime):
- useTime = time.time() - startTime
- return round(useTime, 3)
- #def tic(startTime, name):
- # useTime = time.time() - startTime
- # print('[%s] use time: %1.3f' % (name, useTime))
- def download_urllib(url):
- req = urllib.request.Request(url,
- headers={'user-agent': 'Mozilla/5.0'})
- res = urllib.request.urlopen(req)
- data = res.read()
- try:
- data = data.decode('gbk')
- except UnicodeDecodeError:
- data = data.decode('utf8', 'ignore')
- return res.status, data
- def download_requests(url):
- req = requests.get(url,
- headers={'user-agent': 'Mozilla/5.0'})
- return req.status_code, req.text
- urls = ['http://www.ustchacker.com'] * 10
- urllibL = []
- requestsL = []
- reqPool = []
- reqSpawn = []
- N = 20
- PoolNum = 100
- for i in range(N):
- print('start %d try' % i)
- urllibT = startTimer()
- jobs = [download_urllib(url) for url in urls]
- #for status, data in jobs:
- # print(status, data[:10])
- #tic(urllibT, 'urllib.request')
- urllibL.append(ticT(urllibT))
- print('1')
- requestsT = startTimer()
- jobs = [download_requests(url) for url in urls]
- #for status, data in jobs:
- # print(status, data[:10])
- #tic(requestsT, 'requests')
- requestsL.append(ticT(requestsT))
- print('2')
- requestsT = startTimer()
- pool = gevent.pool.Pool(PoolNum)
- data = pool.map(download_requests, urls)
- #for status, text in data:
- # print(status, text[:10])
- #tic(requestsT, 'requests with gevent.pool')
- reqPool.append(ticT(requestsT))
- print('3')
- requestsT = startTimer()
- jobs = [gevent.spawn(download_requests, url) for url in urls]
- gevent.joinall(jobs)
- #for i in jobs:
- # print(i.value[0], i.value[1][:10])
- #tic(requestsT, 'requests with gevent.spawn')
- reqSpawn.append(ticT(requestsT))
- print('4')
- import matplotlib.pyplot as plt
- x = list(range(1, N+1))
- plt.plot(x, urllibL, label='urllib')
- plt.plot(x, requestsL, label='requests')
- plt.plot(x, reqPool, label='requests geventPool')
- plt.plot(x, reqSpawn, label='requests Spawn')
- plt.xlabel('test number')
- plt.ylabel('time(s)')
- plt.legend()
- plt.show()
运行结果如下:
从上图可以看到,对于I/O密集型任务,gevent还是能对性能做很大提升的,由于协程的创建、调度开销都比线程小的多,所以可以看到不论使用gevent的Spawn模式还是Pool模式,性能差距不大。
因为在gevent中需要使用monkey补丁,会提高gevent的性能,但会影响multiprocessing的运行,如果要同时使用,需要如下代码:
- gevent.monkey.patch_all(thread=False, socket=False, select=False)
可是这样就不能充分发挥gevent的优势,所以不能把multiprocessing Pool、threading Pool、gevent Pool在一个程序中对比。不过比较两图可以得出结论,线程池和gevent的性能最优的,其次是进程池。附带得出个结论,requests库比urllib.request库性能要好一些哈:-)
转载请注明:转自http://blog.csdn.net/littlethunder/article/details/40983031
python3下multiprocessing、threading和gevent性能对比----暨进程池、线程池和协程池性能对比的更多相关文章
- 线程队列 concurrent 协程 greenlet gevent
死锁问题 所谓死锁:是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进 ...
- based on Greenlets (via Eventlet and Gevent) fork 孙子worker 比较 gevent不是异步 协程原理 占位符 placeholder (Future, Promise, Deferred) 循环引擎 greenlet 没有显式调度的微线程,换言之 协程
gevent GitHub - gevent/gevent: Coroutine-based concurrency library for Python https://github.com/gev ...
- python 并发编程 基于gevent模块 协程池 实现并发的套接字通信
基于协程池 实现并发的套接字通信 客户端: from socket import * client = socket(AF_INET, SOCK_STREAM) client.connect(('12 ...
- python3 - 多线程和协程速率测试对比
多线程和协程都属于IO密集型,我通过以下用例测试多线程和协程的实际速率对比. 实例:通过socket客户端以多线程并发模式请求不同服务器端(这里服务器端分2种写法:第一种服务器通过协程实现,第二种服务 ...
- Swoole 同步模式与协程模式的对比
在现代化 PHP 高级开发中,Swoole 为 PHP 带来了更多可能,如:常驻内存.协程,关于传统的 Apache/FPM 模式与常驻内存模式(同步)的巨大差异,之前我做过测试,大家能直观的感受到性 ...
- python采用 多进程/多线程/协程 写爬虫以及性能对比,牛逼的分分钟就将一个网站爬下来!
首先我们来了解下python中的进程,线程以及协程! 从计算机硬件角度: 计算机的核心是CPU,承担了所有的计算任务.一个CPU,在一个时间切片里只能运行一个程序. 从操作系统的角度: 进程和线程,都 ...
- 第十天 多进程、协程(multiprocessing、greenlet、gevent、gevent.monkey、select、selector)
1.多进程实现方式(类似于多线程) import multiprocessing import time,threading def thread_run():#定义一个线程函数 print(&quo ...
- 基础10 多进程、协程(multiprocessing、greenlet、gevent、gevent.monkey、select、selector)
1.多进程实现方式(类似于多线程) import multiprocessing import time,threading def thread_run():#定义一个线程函数 print(&quo ...
- Cpython解释器下实现并发编程——多进程、多线程、协程、IO模型
一.背景知识 进程即正在执行的一个过程.进程是对正在运行的程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所有内容都 ...
随机推荐
- mysql数据库问题———登录进去无法操作显示You must reset your password using ALTER USER statement before executing this statement
linux操作mysql数据库,可以登陆进去,但是操作所有命令都显示You must reset your password using ALTER USER statement before exe ...
- 初次尝试python爬虫,爬取小说网站的小说。
本次是小阿鹏,第一次通过python爬虫去爬一个小说网站的小说. 下面直接上菜. 1.首先我需要导入相应的包,这里我采用了第三方模块的架包,requests.requests是python实现的简单易 ...
- 环境变量和Path环境变量
环境变量 百度百科下的定义 一般是指在操作系统中用来指定操作系统运行环境的一些参数,如:临时文件夹位置和系统文件夹位置等. 环境变量是在操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所 ...
- js日期格式验证
js日期格式验证 <input type="text" maxLength='10' onkeyup='checkDate(this.value,jQuery(this)); ...
- HTML回顾之表格
HTML表格 由什么组成? 表格由<table>标签来定义.每个表格有若干行(<tr>标签来定义),每行被分割成若干单元格(<td>标签来定义). td值表格数据, ...
- 工具使用——IDEA常用的几种插件
Rainbow Brackets:彩虹颜色的括号 Maven Helper :分析依赖冲突插件 Grep Console:显示不同日志级别不同颜色 Mybatis Log Plugin:直接将Myba ...
- 关于Mysql5.6 Failed to open file error2的记录
今天在执行mysql命令行下的\.命令时,它总说Failed to open file “...........”error2 找了半天原因,最后发现是百度云在往那个文件夹里下载东西,所以访问文件的时 ...
- 202-基于TI DSP TMS320C6678、Xilinx K7 FPGA XC7K325T的高速数据处理核心板
该DSP+FPGA高速信号采集处理板由我公司自主研发,包含一片TI DSP TMS320C6678和一片Xilinx FPGA K7 XC72K325T-1ffg900.包含1个千兆网口,1个FMC ...
- Codeforces Round #430 (Div. 2) - D
题目链接:http://codeforces.com/contest/842/problem/D 题意:定义Mex为一个序列中最小的未出现的正整数,给定一个长度为n的序列,然后有m个询问,每个询问给定 ...
- tf中的meta和pb
https://www.jianshu.com/p/af2b694eae61 简单来说,pb文件是个推理的图,meta更复杂,不能用来部署