Python Multiprocessing - How to pass kwargs to function?

后端 未结 1 713
说谎
说谎 2020-12-15 23: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

相关标签:
1条回答
  • 2020-12-15 23:32

    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)
    
    0 讨论(0)
提交回复
热议问题