标签:
1、纯计算 多线程只用了一个cpu 45%左右 多进程用两个cpu 90%左右
# 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 # 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
2、涉及io 多线程、多进程都用两个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
标签:
原文地址:http://www.cnblogs.com/nerrissa/p/5603372.html