无法使用python-2.7将生成器的输出映射到多处理中的函数

时间:2016-10-04 17:23:27

标签: python python-2.7 multiprocessing

这是我的代码:

from multiprocessing import Pool

user_list = [1, 2, 3, 4, 5]
def gen_pair():
    for u1 in reversed(user_list):
        for u2 in reversed(list(range(1, u1))):
            yield (u1, u2)

def cal_sim(u_pair):
     u1, u2 = u_pair
     sim = sim_f(df[u1], df[u2])
     return sim

pool = Pool(processes=6)
vals = pool.map(cal_sim, gen_pair())
df2record = pd.DataFrame(columns=['u1', 'u2', 'js'])
for v in vals:
    print (v)
pool.terminate()

但是当我运行代码时,我遇到了这样的错误:TypeError: unsupported operand type(s) for +: 'set' and 'set'。完整的TraceBack如下:

Traceback (most recent call last):
  File "b.py", line 57, in <module>
    main()
  File "b.py", line 47, in main
    vals = pool.map(cal_sim, gen_pair())
  File "yobichi/python/2.7.10_2/lib/python2.7/multiprocessing/pool.py", line 251, in map
    return self.map_async(func, iterable, chunksize).get()
  File "yobichi/python/2.7.10_2/lib/python2.7/multiprocessing/pool.py", line 567, in get
    raise self._value
TypeError: unsupported operand type(s) for +: 'set' and 'set'

您能否告诉我原因是什么?如何妥善处理?

1 个答案:

答案 0 :(得分:0)

这里有一个无限递归:

def cal_sim(u_pair):
    u1, u2 = u_pair
    sim = cal_sim(df[u1], df[u2])
    return sim

因为cal_sim调用cal_sim而没有任何结束条件。

参数也有问题,因为cal_sim有1个参数,你用2个参数调用它。