用于多个参数的Python多处理池.map在Python多处理库中,是否存在支持多个参数的pool.map的变体?text = "test"def harvester(text, case):
X = case[0]
text+ str(X)if __name__ == '__main__':
pool = multiprocessing.Pool(processes=6)
case = RAW_DATASET
pool.map(harvester(text,case),case, 1)
pool.close()
pool.join()
3 回答
慕田峪7331174
TA贡献1828条经验 获得超13个赞
Pool.starmap
import multiprocessingfrom itertools import productdef merge_names(a, b): return '{} & {}'.format(a, b)if __name__ == '__main__': names = ['Brown', 'Wilson', 'Bartlett', 'Rivera', 'Molloy', 'Opie'] with multiprocessing.Pool(processes=3) as pool: results = pool.starmap(merge_names, product(names, repeat=2)) print(results)# Output: ['Brown & Brown', 'Brown & Wilson', 'Brown & Bartlett', ...
with
Pool
import multiprocessingfrom itertools import productfrom contextlib import contextmanagerdef merge_names(a, b): return '{} & {}'.format(a, b)def merge_names_unpack(args): return merge_names(*args)@contextmanagerdef poolcontext(*args, **kwargs): pool = multiprocessing.Pool(*args, **kwargs) yield pool pool.terminate()if __name__ == '__main__': names = ['Brown', 'Wilson', 'Bartlett', 'Rivera', 'Molloy', 'Opie'] with poolcontext(processes=3) as pool: results = pool.map(merge_names_unpack, product(names, repeat=2)) print(results)# Output: ['Brown & Brown', 'Brown & Wilson', 'Brown & Bartlett', ...
partial
import multiprocessingfrom functools import partialfrom contextlib import contextmanager@contextmanagerdef poolcontext(*args, **kwargs): pool = multiprocessing.Pool(*args, **kwargs) yield pool pool.terminate()def merge_names(a, b): return '{} & {}'.format(a, b)if __name__ == '__main__': names = ['Brown', 'Wilson', 'Bartlett', 'Rivera', 'Molloy', 'Opie'] with poolcontext(processes=3) as pool: results = pool.map(partial(merge_names, b='Sons'), names) print(results)# Output: ['Brown & Sons', 'Wilson & Sons', 'Bartlett & Sons', ...
红糖糍粑
TA贡献1815条经验 获得超6个赞
是否有池.map的变体支持多个参数?
pool.starmap()
#!/usr/bin/env python3from functools import partialfrom itertools import repeatfrom multiprocessing import Pool, freeze_supportdef func(a, b): return a + bdef main(): a_args = [1,2,3] second_arg = 1 with Pool() as pool: L = pool.starmap(func, [(1, 1), (2, 1), (3, 1)]) M = pool.starmap(func, zip(a_args, repeat(second_arg))) N = pool.map(partial(func, b=second_arg), a_args) assert L == M == Nif __name__=="__main__": freeze_support() main()
#!/usr/bin/env python2import itertoolsfrom multiprocessing import Pool, freeze_supportdef func(a, b): print a, bdef func_star(a_b): """Convert `f([1,2])` to `f(1,2)` call.""" return func(*a_b)def main(): pool = Pool() a_args = [1,2,3] second_arg = 1 pool.map(func_star, itertools.izip(a_args, itertools.repeat(second_arg)))if __name__=="__main__": freeze_support() main()
输出量
1 1 2 1 3 1
itertools.izip()
itertools.repeat()
functools.partial()
func_star()
uptimebox
.
慕娘9325324
TA贡献1783条经验 获得超4个赞
def multi_run_wrapper(args): return add(*args)def add(x,y): return x+yif __name__ == "__main__": from multiprocessing import Pool pool = Pool(4) results = pool.map(multi_run_wrapper,[(1,2),(2,3),(3,4)]) print results
[3, 5, 7]
添加回答
举报
0/150
提交
取消