如何使用 Python 的多处理将字典传递给函数?文档:https://docs.python.org/3.4/library/multiprocessing.html#reference 说要传递字典,但我不断得到
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'
代码如下:
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)
当我运行你的代码时,我得到了一个不同的错误:
When I ran your code, I got a different error:
TypeError: fp() takes at most 3 arguments (5 given)
我通过打印 args 和 kwargs 并将方法更改为 fp(*args, **kwargs) 进行了调试,并注意到bob_"被作为一个字母数组传入.用于 args 的括号似乎是可操作的,实际上并没有给你一个元组.将其更改为列表,然后将 numList 作为关键字参数传入,使代码对我有用.
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)
这篇关于Python 多处理 - 如何将 kwargs 传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
Python 多处理模块的 .join() 方法到底在做什么?What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多处理模块的 .join() 方法到底在做什么?)
在 Python 中将多个参数传递给 pool.map() 函数Passing multiple parameters to pool.map() function in Python(在 Python 中将多个参数传递给 pool.map() 函数)
multiprocessing.pool.MaybeEncodingError: 'TypeError("multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEnc
Python 多进程池.当其中一个工作进程确定不再需要Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多进程池.当其中一
如何将队列引用传递给 pool.map_async() 管理的函数How do you pass a Queue reference to a function managed by pool.map_async()?(如何将队列引用传递给 pool.map_async() 管理的函数?)
与多处理错误的另一个混淆,“模块"对象没yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(与多处理错误的另一个混淆,“模块对象