ThreadPool包含类列表和成员函数

时间:2017-02-09 11:03:31

标签: python multithreading threadpool

我有一个函数,我想用pool创建multiThreaded。

def find(item):
    curve=Curve(item)
    return curve._find()

多线程版本wouuld检查输入是否为列表:

def find(item):
    if type(item) == list:
    items=item
    pool = ThreadPool(len(items))
    curves = pool.map(Curve, moniker)
    pool.close()

    pool = ThreadPool(len(items))

    # now comes the tricky part:
    results = pool.map(???) # curves is a list of class 
                            # with each having _find as a function

    pool.close()
    return results

    else:
        curve=Curve(item)
        return curve._find()

如何使用上面描述的类列表调用pool.map?

1 个答案:

答案 0 :(得分:1)

如果我明白了,你只需要声明一个函数来映射列表中的项目:

def find(item):
    def find_(item):
        curve=Curve(item)
        return curve._find()
    if type(item) == list:
        items=item

        pool = ThreadPool(len(items))

        # now comes the tricky part:
        results = pool.map(find_, items) # curves is a list of class 
                                          # with each having _find as a function

        pool.close()
        return results

    else:
        return find_(item)