Python Multiprocessing - How to pass kwargs to function?
Asked Answered
P

2

19

How do I pass a dictionary to a function with Python's Multiprocessing? The Documentation: https://docs.python.org/3.4/library/multiprocessing.html#reference says to pass a dictionary, but I keep getting

TypeError: fp() got multiple values for argument 'what'

Here's the code:

from multiprocessing import Process, Manager
   
def fp(name, numList=None, what='no'):
        print ('hello %s %s'% (name, what))
        numList.append(name+'44')
 
if __name__ == '__main__':

    manager = Manager()
 
    numList = manager.list()
    for i in range(10):
        keywords = {'what':'yes'}
        p = Process(target=fp, args=('bob'+str(i)), kwargs={'what':'yes'})
        p.start()
        print("Start done")
        p.join()
        print("Join done")
    print (numList)
Patterson answered 12/8, 2016 at 1:54 Comment(2)
I believe that this thread will help answer your question.Aggy
Also here: #34032181Discrimination
L
30

When I ran your code, I got a different error:

TypeError: fp() takes at most 3 arguments (5 given)

I debugged by printing args and kwargs and changing the method to fp(*args, **kwargs) and noticed that "bob_" was being passed in as an array of letters. It seems that the parentheses used for args were operational and not actually giving you a tuple. Changing it to the list, then also passing in numList as a keyword argument, made the code work for me.

from multiprocessing import Process, Manager

def fp(name, numList=None, what='no'):
    print ('hello %s %s' % (name, what))
    numList.append(name+'44')

if __name__ == '__main__':

    manager = Manager()

    numList = manager.list()
    for i in range(10):
        keywords = {'what': 'yes', 'numList': numList}
        p = Process(target=fp, args=['bob'+str(i)], kwargs=keywords)
        p.start()
        print("Start done")
        p.join()
        print("Join done")
    print (numList)
Lesser answered 12/8, 2016 at 2:13 Comment(1)
Thanks! I didn't think it was possible and ended up coding it a different way. But I'll use this in the future! Also adding a comma after 'bob'+str(i) makes it a list 'bob'+str(i),Patterson
C
1

You are missing a comma: args=('bob'+str(i)) -> args=('bob'+str(i), ) otherwise Python would recognize the expression as a tuple.

Convolute answered 11/1, 2024 at 8:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.