具有更多参数的池映射

时间:2015-12-03 11:33:05

标签: python multithreading dictionary

如果第一个参数是list必须迭代而第二个shoud在每个函数调用中保持不变,如何映射2个参数?

以下是没有地图的示例,以显示我的真实含义:

x = "something"
pool = Pool(4)
for code in get_codes():
    pool.apply_async(execute, args=(code,x))

pool.close()
pool.join()

现在代码使用map

pool = Pool(4)
pool.map(execute, WHAT TO PUT HERE)?

pool.close()
pool.join()

示例:

def print_it(x,y):
    print x,y

xs = [0,1,2,3,4,5,6]

pool = Pool(3)

pool.map(""" map print_it on xs and y as '0'""")

pool.close()
pool.join()

1,0
0,0
2,0
3,0
5,0
4,0
6,0

1 个答案:

答案 0 :(得分:1)

您可以使用functools.partial

def print_it(y,x):
    print x,y

xs = [0,1,2,3,4,5,6]
y = 0

func = partial(print_it, y)

pool = Pool(3)
pool.map(func, xs)

pool.close()
pool.join()

1,0
0,0
2,0
3,0
5,0
4,0
6,0