如果我具有以下功能:
def foo(a, b, c=2, d=6):
return a + b + c + d
,我想与multiprocessing
并行化。
如何传递以下参数1, 2, d=10
?即2 args
和1 kwarg
?
答案 0 :(得分:1)
怎么样:
import multiprocessing
def foo(a, b, c=2, d=6):
return a + b + c + d
def foo_callback(result):
print(result)
def foo_error(error):
raise error
pool = multiprocessing.Pool()
for (a, b, c, d) in ((1, 2, 3, 4),
(2, 4, 6, 8),
(3, 6, 9, 12)):
pool.apply_async(
foo,
args=(a, b),
kwds={"c":c, "d":d},
callback=foo_callback,
error_callback=foo_error
)
pool.close()
pool.join()
哪些印刷品:
10
20
30
Process finished with exit code 0
我猜你想通过kwds作为命令。密钥必须是字符串。
答案 1 :(得分:1)