具有不同功能的多进程池

具有不同功能的多进程池,第1张

具有不同功能的多进程池

要传递不同的功能,您可以简单地

map_async
多次调用

这里有一个例子来说明这一点

from multiprocessing import Poolfrom time import sleepdef square(x):    return x * xdef cube(y):    return y * y * ypool = Pool(processes=20)result_squares = pool.map_async(f, range(10))result_cubes = pool.map_async(g, range(10))

结果将是:

>>> print result_squares.get(timeout=1)[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]>>> print result_cubes.get(timeout=1)[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5629572.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存