如何使用Python的Multiprocessing将字典传递给函数?文档:https : //docs.python.org/3.4/library/multiprocessing.html#reference说要通过字典,但是我一直在
TypeError: fp() got multiple values for argument 'what'
这是代码:
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)
当我运行您的代码时,出现了另一个错误:
TypeError: fp() takes at most 3 arguments (5 given)
我通过打印args和kwargs并将方法更改为进行调试,fp(*args, **kwargs)并注意到“ bob_”作为字母数组传入。似乎括号args是可操作的,实际上并没有给您元组。将其更改为列表,然后还numList作为关键字参数传入,使代码对我有用。
fp(*args, **kwargs)
args
numList
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)