【发布时间】:2026-01-22 15:10:01
【问题描述】:
为什么内置映射可以工作,而多处理的 ThreadPool 映射却不能工作?
from multiprocessing.pool import ThreadPool
def identity(a, b): return (a, b)
map(identity, [1, 2, 3], [4, 5, 6])
p = ThreadPool(2)
#gives above error:
p.map(identity, [1, 2, 3], [4, 5, 6])
编辑: 经过一番挖掘,显然线程池的映射不支持 vararg 样式的映射,即 map(f, i1, i2, i3,...in) 其中 i1 对应于 f 的第一个参数, i2 第二个等。抛出异常是因为我给它的列表被解释为块大小或其他整数位置参数。
无论如何,简洁的解决方案将不胜感激。
【问题讨论】:
标签: python multithreading threadpool