1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#_*_coding:utf-8_*_ import time
from multiprocessing import Pool
from threading import Thread
def func1(fn):
time.sleep( 1 )
return fn * fn
if __name__ = = "__main__" :
a = [ 1 , 2 , 3 , 4 , 5 , 6 ]
print "顺序执行的方式开始..."
s = time.time()
for i in a:
func1(i)
el = time.time()
print "顺序执行时间为:" , int (el - s)
print "创建多个进程,并行执行开始"
pool = Pool( 5 ) #创建拥有5个进程数量的进程池,也就是说可以同时跑5个线程
p1 = pool. map (func1,a)
pool.close() #关闭进程池,不再接受新的进程
pool.join() #主进程阻塞等待子进程的退出
e3 = time.time()
print "多进程并行时间为:" , int (e3 - el)
print p1
|
本文转自 TtrToby 51CTO博客,原文链接:http://blog.51cto.com/freshair/1899772