#_*_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
时间: 2024-12-28 12:59:43