如何在课堂上使用多处理?

时间:2018-02-20 09:59:47

标签: python class multiprocess

我想使用多处理来执行以下操作:

class myClass:

    def proc(self):
        #processing random numbers
        return a

    def gen_data(self):
        with Pool(cpu_count()) as q:
            data = q.map(self.proc, [_ for i in range(cpu_count())])#What is the correct approach?
        return data

2 个答案:

答案 0 :(得分:0)

试试这个:

def proc(self, i):
    #processing random numbers
    return a

def gen_data(self):
    with Pool(cpu_count()) as q:
        data = q.map(self.proc, [i for i in range(cpu_count())])#What is the correct approach?
    return data

答案 1 :(得分:0)

由于您不必将参数传递给流程,因此没有理由map,只需根据需要多次调用apply_async()

这就是我所说的:

from multiprocessing import cpu_count
from multiprocessing.pool import Pool
from random import randint

class MyClass:

    def proc(self):
        #processing random numbers
        return randint(1, 10)

    def gen_data(self, num_procs):
        with Pool() as pool:  # The default pool size will be the number of cpus.
            results = [pool.apply_async(self.proc) for _ in range(num_procs)]
            pool.close()
            pool.join()  # Wait until all worker processes exit.

        return [result.get() for result in results]  # Gather results.

if __name__ == '__main__':
    obj = MyClass()
    print(obj.gen_data(8))
相关问题