具有 类 列表和成员函数的线程池

ThreadPool with list of classes and member function

我有一个函数,我想用池实现多线程。

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

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

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?

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

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)