并发编程(二)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()
posted @ 2021-06-14 23:49  我是一言  阅读(410)  评论(0编辑  收藏  举报