1、多线程:开启一个进程test.py ,占用两个cpu 共占用45%左右(top -c ,按1) 多进程:开启两个进程test.py 用两个cpu 90%*2左右
test.py
# coding=utf-8 from multiprocessing import Pool # 多线程版本from multiprocessing.dummy import Pool
import sys, os
import time def timetask(times):
while 1:
pass if __name__ == '__main__':
startTime = time.time()
worknum = 2
processnum = 2
pool = Pool(processnum)
for i in range(worknum):
pool.apply_async(timetask, (i, ))
pool.close()
pool.join()
endTime = time.time()
print "time :", endTime - startTime
2、多线程:开启一个进程test.py ,两个gzip进程 占用两个cpu 多进程:开启两个进程test.py ,两个gzip进程 (主进程test.py S状态)
多线程、多进程都用两个cpu 90%左右
# coding=utf-8 from multiprocessing import Pool
import sys, os
import time def timetask(times):
cmd="tar zcfPh /nerri_%s.tar.gz /tmp" % times
os.system(cmd) if __name__ == '__main__':
startTime = time.time()
worknum = 2 # time : 55.9383509159
processnum = 2 # 进程版本 processnum:2, time : 55.9383509159;processnum:1,time : 108.528738976 ;线程版本 processnum:2,time : 55.0623002052
pool = Pool(processnum)
for i in range(worknum):
pool.apply_async(timetask, (i, ))
pool.close()
pool.join()
endTime = time.time()
print "time :", endTime - startTime