【发布时间】:2017-10-12 23:03:27
【问题描述】:
这个问题可能是重复的。但是,我阅读了很多关于这个主题的内容,但没有找到与我的情况相匹配的内容 - 或者至少,我不理解它。
很抱歉给您带来不便。
我正在尝试做的事情相当普遍,将 kwargs 列表传递给 pool.starmap(),以实现多处理。
这是我的简化案例:
def compute(firstArg, **kwargs): # A function that does things
# Fancy computing stuff...
# Saving to disk...
return True
if __name__ == '__main__':
args = [{
'firstArg': "some data",
'otherArg': "some other data"
'andAnotherArg': x*2
} for x in range(42)]
pool = Pool(4)
pool.starmap(compute, args)
pool.close()
pool.terminate()
我认为 starmap() 将解压 dict 并将其作为关键字 args 传递给 compute(),但查看 source code(另见 l.46),它只发送键(或值?)。
所以它提出了:
TypeError: compute() takes 1 positional argument but 3 were given
这必须是一种清晰、直接的方式来做到这一点...任何帮助将不胜感激。
这是一个非常相似的问题:Python Multiprocessing - How to pass kwargs to function?
【问题讨论】:
标签: python multiprocessing pool