【问题标题】:ThreadPool with list of classes and member function具有类列表和成员函数的 ThreadPool
【发布时间】:2017-02-09 11:03:30
【问题描述】:

我有一个函数,我想用池创建多线程。

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?

【问题讨论】:

    标签: python multithreading threadpool


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 2013-05-03
      • 1970-01-01
      相关资源
      最近更新 更多