0

How to map 2 arguments if the firs argument is list which has to iterate and the second shoud stay the same in each function call?

Here is the example without map to show what I really mean:

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

pool.close()
pool.join()

Now the code using map:

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

pool.close()
pool.join()

Example:

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
Milano
  • 18,048
  • 37
  • 153
  • 353
  • There's plenty of material for this on SO - check out [this](http://stackoverflow.com/questions/25553919/passing-multiple-parameters-to-pool-map-function-in-python) and [this](http://stackoverflow.com/questions/8521883/multiprocessing-pool-map-and-function-with-two-arguments) thread for different ways to address the issue. – Daniel Lenz Dec 03 '15 at 13:27

1 Answers1

1

You can use 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
eponym0us
  • 11
  • 3