python的多进程性能要明显优于多线程,因为cpython的gil对性能做了约束。
python是运行在解释器中的语言,查找资料知道,python中有一个全局锁(gil),在使用多进程(thread)的情况下,不能发挥多核的优势。而使用多进程(multiprocess),则可以发挥多核的优势真正地提高效率。
对比实验
资料显示,如果多线程的进程是cpu密集型的,那多线程并不能有多少效率上的提升,相反还可能会因为线程的频繁切换,导致效率下降,推荐使用多进程;如果是io密集型,多线程进程可以利用io阻塞等待时的空闲时间执行其他线程,提升效率。所以我们根据实验对比不同场景的效率
操作系统 | cpu | 内存 | 硬盘 |
---|---|---|---|
windows 10 | 双核 | 8gb | 机械硬盘 |
(1)引入所需要的模块
1
2
3
4
|
import requests
import time
from threading import thread
from multiprocessing import process
|
(2)定义cpu密集的计算函数
1
2
3
4
5
6
7
|
def count(x, y):
# 使程序完成150万计算
c = 0
while c < 500000 :
c + = 1
x + = x
y + = y
|
(3)定义io密集的文件读写函数
1
2
3
4
5
6
7
8
9
10
|
def write():
f = open ( "test.txt" , "w" )
for x in range ( 5000000 ):
f.write( "testwrite\n" )
f.close()
def read():
f = open ( "test.txt" , "r" )
lines = f.readlines()
f.close()
|
(4) 定义网络请求函数
1
2
3
4
5
6
7
8
9
10
|
_head = {
'user-agent' : 'mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/48.0.2564.116 safari/537.36' }
url = "http://www.tieba.com"
def http_request():
try :
webpage = requests.get(url, headers = _head)
html = webpage.text
return { "context" : html}
except exception as e:
return { "error" : e}
|
(5)测试线性执行io密集操作、cpu密集操作所需时间、网络请求密集型操作所需时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# cpu密集操作
t = time.time()
for x in range ( 10 ):
count( 1 , 1 )
print ( "line cpu" , time.time() - t)
# io密集操作
t = time.time()
for x in range ( 10 ):
write()
read()
print ( "line io" , time.time() - t)
# 网络请求密集型操作
t = time.time()
for x in range ( 10 ):
http_request()
print ( "line http request" , time.time() - t)
|
输出
cpu密集:95.6059999466、91.57099986076355 92.52800011634827、 99.96799993515015
io密集:24.25、21.76699995994568、21.769999980926514、22.060999870300293
网络请求密集型: 4.519999980926514、8.563999891281128、4.371000051498413、4.522000074386597、14.671000003814697
(6)测试多线程并发执行cpu密集操作所需时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
counts = []
t = time.time()
for x in range ( 10 ):
thread = thread(target = count, args = ( 1 , 1 ))
counts.append(thread)
thread.start()
e = counts.__len__()
while true:
for th in counts:
if not th.is_alive():
e - = 1
if e < = 0 :
break
print (time.time() - t)
|
output: 99.9240000248 、101.26400017738342、102.32200002670288
(7)测试多线程并发执行io密集操作所需时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
def io():
write()
read()
t = time.time()
ios = []
t = time.time()
for x in range ( 10 ):
thread = thread(target = count, args = ( 1 , 1 ))
ios.append(thread)
thread.start()
e = ios.__len__()
while true:
for th in ios:
if not th.is_alive():
e - = 1
if e < = 0 :
break
print (time.time() - t)
|
output: 25.69700002670288、24.02400016784668
(8)测试多线程并发执行网络密集操作所需时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
t = time.time()
ios = []
t = time.time()
for x in range ( 10 ):
thread = thread(target = http_request)
ios.append(thread)
thread.start()
e = ios.__len__()
while true:
for th in ios:
if not th.is_alive():
e - = 1
if e < = 0 :
break
print ( "thread http request" , time.time() - t)
|
output: 0.7419998645782471、0.3839998245239258、0.3900001049041748
(9)测试多进程并发执行cpu密集操作所需时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
counts = []
t = time.time()
for x in range ( 10 ):
process = process(target = count, args = ( 1 , 1 ))
counts.append(process)
process.start()
e = counts.__len__()
while true:
for th in counts:
if not th.is_alive():
e - = 1
if e < = 0 :
break
print ( "multiprocess cpu" , time.time() - t)
|
output: 54.342000007629395、53.437999963760376
(10)测试多进程并发执行io密集型操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
t = time.time()
ios = []
t = time.time()
for x in range ( 10 ):
process = process(target = io)
ios.append(process)
process.start()
e = ios.__len__()
while true:
for th in ios:
if not th.is_alive():
e - = 1
if e < = 0 :
break
print ( "multiprocess io" , time.time() - t)
|
output: 12.509000062942505、13.059000015258789
(11)测试多进程并发执行http请求密集型操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
t = time.time()
httprs = []
t = time.time()
for x in range ( 10 ):
process = process(target = http_request)
ios.append(process)
process.start()
e = httprs.__len__()
while true:
for th in httprs:
if not th.is_alive():
e - = 1
if e < = 0 :
break
print ( "multiprocess http request" , time.time() - t)
|
output: 0.5329999923706055、0.4760000705718994
实验结果
cpu密集型操作 | io密集型操作 | 网络请求密集型操作 | |
---|---|---|---|
线性操作 | 94.91824996469 | 22.46199995279 | 7.3296000004 |
多线程操作 | 101.1700000762 | 24.8605000973 | 0.5053332647 |
多进程操作 | 53.8899999857 | 12.7840000391 | 0.5045000315 |
通过上面的结果,我们可以看到:
多线程在io密集型的操作下似乎也没有很大的优势(也许io操作的任务再繁重一些就能体现出优势),在cpu密集型的操作下明显地比单线程线性执行性能更差,但是对于网络请求这种忙等阻塞线程的操作,多线程的优势便非常显著了
多进程无论是在cpu密集型还是io密集型以及网络请求密集型(经常发生线程阻塞的操作)中,都能体现出性能的优势。不过在类似网络请求密集型的操作上,与多线程相差无几,但却更占用cpu等资源,所以对于这种情况下,我们可以选择多线程来执行
以上所述是小编给大家介绍的python单线程多线程和多进程效率对比详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/Jailman/article/details/78427936