并发编程(二)multiprocessing.dummy.Pool

multiprocessing.dummy.Pool

import time
from multiprocessing.dummy import Pool

def run(fn):
    time.sleep(2)
    print(fn)

if __name__=="__main__":
    testFL=[1,2,3,4,5]  # 迭代对象
    pool=Pool(10) # 创建10个容量的线程池并发执行
    pool.map(run,testFL)
    pool.close()
    pool.join()

map函数源码

    def map(self, func, iterable, chunksize=None):
        '''
        Apply `func` to each element in `iterable`, collecting the results
        in a list that is returned.
        '''
        return self._map_async(func, iterable, mapstar, chunksize).get()
更多学习笔记移步 https://www.cnblogs.com/kknote
原文地址:https://www.cnblogs.com/kknote/p/14883825.html