0

I am trying to run a python function on multiple cores on my CPU but I keep getting this error that says 'need more than 1 value to unpack'.

I am passing two arguments to map function the first argument is my function which I want to run on multiple cores and the second argument is a list of tuples which I want to pass as an argument to my function.

something like this

def func(list_obj):
   temp = list_obj[0]
   img = list_obj[1]


arg = list()
arg.append((img1,img2))

pool = multiprocessing.Pool(processes = 2)
results = pool.map(func,arg)
print (results)

Can someone please help me why am I getting this error and what possible way is there to solve this error

Razor
  • 89
  • 9
  • If you do `arg.append(img1)` then `arg.append(img2)` on separate lines does that work? I think your argument might be `((img1, img2))` as opposed to `(img1, img2)` which is what your function is expecting? – Ari Cooper-Davis May 13 '17 at 11:46
  • The arguments that needs to be passed in map function must be some iterable, If you look closely for every process the argument is divided into further place holders like list_obj[0], list_obj[1] etc. My program takes argument in the form of a tuple like (img1,img2). – Razor May 13 '17 at 12:13
  • What line are you getting the error on? – Ari Cooper-Davis May 13 '17 at 12:15
  • I am getting error here: results = pool.map(func,arg) – Razor May 13 '17 at 12:16
  • 1
    Do a debug print of those arguments to verify there IS more than 1 value to unpack. – handle May 13 '17 at 12:19
  • Docs: https://docs.python.org/3.6/library/multiprocessing.html#multiprocessing.pool.Pool.map, related: http://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments – handle May 13 '17 at 12:21
  • I tried debugging it by printing the values, but I see no issue there. Can it be possible that reading images through opencv changes them to array object and it is there what is causing an error? – Razor May 13 '17 at 12:27
  • `print(type(img1))`!? – handle May 13 '17 at 12:28
  • returns numpy.ndarray – Razor May 13 '17 at 12:30
  • Well, does that not answer your previous question? See my answer for a snippet you could build on. Good luck! – handle May 13 '17 at 12:49
  • I guess i was looking at the problem the wrong way all the time, I was able to come up with solution it seems that the error was not actually in passing arguments, but in execution of function. I was making a wrong call of first raveling my list and then using the shape function and this is where i was getting the error from. – Razor May 13 '17 at 20:24

1 Answers1

0

I won't be able to help, but here is a minimal code that does not produce an error and seems to run the map properly (but I don't know how the results are supposed to be returned):

import multiprocessing

results = []

def func(list_obj):
    print(list_obj)
    temp = list_obj[0]
    img = list_obj[1]
    results.append( {"temp":temp, "img":img} )
    return True


if __name__=="__main__":

    iterable = list()
    iterable.append(("some",123))
    iterable.append(("data",456))
    iterable.append(("to",789))
    iterable.append(("play",789))
    iterable.append(("with","0ab"))
    print(iterable)


    pool = multiprocessing.Pool(processes = 2)
    pool.map(func, iterable)
    print(results)

Maybe you'll get somewhere with building on something small that works until it breaks - then you've got code to share and get help with.

handle
  • 5,859
  • 3
  • 54
  • 82