小编Got*_*tte的帖子

在类方法Python中调用多处理

最初,我有一个类来存储一些处理过的值,并使用其他方法重用它们.

问题是当我试图将类方法划分为多个进程以加速,python生成的进程但似乎不起作用(正如我在任务管理器中看到的那样只有一个进程正在运行)并且结果永远不会被传递.

我做了几次搜索,发现pathos.multiprocessing可以做到这一点,但我想知道标准库是否可以解决这个问题?

from multiprocessing import Pool

class A():
    def __init__(self, vl):
        self.vl = vl
    def cal(self, nb):
        return nb * self.vl
    def run(self, dt):
        t = Pool(processes=4)
        rs = t.map(self.cal, dt)
        t.close()
        return t

a = A(2)

a.run(list(range(10)))
Run Code Online (Sandbox Code Playgroud)

python methods multiprocessing

4
推荐指数
1
解决办法
5779
查看次数

标签 统计

methods ×1

multiprocessing ×1

python ×1