Passing multiple parameters to pool.map() function in Python
本问题已经有最佳答案,请猛点这里访问。
我需要一些方法来使用pool.map()中接受多个参数的函数。根据我的理解,pool.map()的目标函数只能有一个iterable作为参数,但是有没有其他参数也可以传入的方法?在这种情况下,我需要传递一些配置变量,比如锁(),并将信息记录到目标函数中。
我试过做一些研究,我认为我可以使用部分函数使它工作?但是我不完全理解这些是如何工作的。任何帮助都将不胜感激!下面是一个简单的例子,说明我想做什么:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def target(items, lock): for item in items: # Do cool stuff if (... some condition here ...): lock.acquire() # Write to stdout or logfile, etc. lock.release() def main(): iterable = [1, 2, 3, 4, 5] pool = multiprocessing.Pool() pool.map(target(PASS PARAMS HERE), iterable) pool.close() pool.join() |
您可以使用EDOCX1[0]进行此操作(如您怀疑的那样):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from functools import partial def target(lock, iterable_item): for item in iterable_item: # Do cool stuff if (... some condition here ...): lock.acquire() # Write to stdout or logfile, etc. lock.release() def main(): iterable = [1, 2, 3, 4, 5] pool = multiprocessing.Pool() l = multiprocessing.Lock() func = partial(target, l) pool.map(func, iterable) pool.close() pool.join() |
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def f(a, b, c): print("{} {} {}".format(a, b, c)) def main(): iterable = [1, 2, 3, 4, 5] pool = multiprocessing.Pool() a ="hi" b ="there" func = partial(f, a, b) pool.map(func, iterable) pool.close() pool.join() if __name__ =="__main__": main() |
号
输出:
1 2 3 4 5 | hi there 1 hi there 2 hi there 3 hi there 4 hi there 5 |
您可以使用允许多个参数的map函数,就像在
1 2 3 4 5 6 7 8 9 10 | >>> from pathos.multiprocessing import ProcessingPool as Pool >>> >>> def add_and_subtract(x,y): ... return x+y, x-y ... >>> res = Pool().map(add_and_subtract, range(0,20,2), range(-5,5,1)) >>> res [(-5, 5), (-2, 6), (1, 7), (4, 8), (7, 9), (10, 10), (13, 11), (16, 12), (19, 13), (22, 14)] >>> Pool().map(add_and_subtract, *zip(*res)) [(0, -10), (4, -8), (8, -6), (12, -4), (16, -2), (20, 0), (24, 2), (28, 4), (32, 6), (36, 8)] |
1 2 3 4 5 | >>> from pathos.multiprocessing import ThreadingPool as TPool >>> >>> res = TPool().amap(add_and_subtract, *zip(*Pool().map(add_and_subtract, range(0,20,2), range(-5,5,1)))) >>> res.get() [(0, -10), (4, -8), (8, -6), (12, -4), (16, -2), (20, 0), (24, 2), (28, 4), (32, 6), (36, 8)] |
。
更有趣的是,构建一个可以传递到池中的嵌套函数。这是可能的,因为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | >>> def build_fun_things(f, g): ... def do_fun_things(x, y): ... return f(x,y), g(x,y) ... return do_fun_things ... >>> def add(x,y): ... return x+y ... >>> def sub(x,y): ... return x-y ... >>> neato = build_fun_things(add, sub) >>> >>> res = TPool().imap(neato, *zip(*Pool().map(neato, range(0,20,2), range(-5,5,1)))) >>> list(res) [(0, -10), (4, -8), (8, -6), (12, -4), (16, -2), (20, 0), (24, 2), (28, 4), (32, 6), (36, 8)] |
。
但是,如果您不能走出标准库,您将不得不以另一种方式进行。在这种情况下,最好的选择是使用
获取
如果您没有访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | def target(lock): def wrapped_func(items): for item in items: # Do cool stuff if (... some condition here ...): lock.acquire() # Write to stdout or logfile, etc. lock.release() return wrapped_func def main(): iterable = [1, 2, 3, 4, 5] pool = multiprocessing.Pool() lck = multiprocessing.Lock() pool.map(target(lck), iterable) pool.close() pool.join() |
。
这使得