- 先看一个小例子:
import time
def task():
time.sleep(1)
start = time.time()
for i in range(100000):
task()
end = time.time()
print '串行所用时间:%d 秒'%(end-start)
程序输出:串行所用时间:100000 秒
- 使用multiprocessing并行执行
from multiprocessing import Pool
import time
def task():
time.sleep(1)
start = time.time()
pool = Pool()
for i in range(100000):
pool.apply_async(task)
end = time.time()
print '并行所用时间:%d 秒'%(end-start)
pool.close()
pool.join()
程序输出:并行所用时间:3 秒