如何将多重处理与继承结合使用?

2024-10-03 21:35:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试通过多重处理来加速脚本的运行时间。当我用更简单的定义(如调整不同目录上的图像大小)尝试相同的多处理代码时,多处理效果很好,但当我用下面看到的代码尝试时,它运行了,但没有给出任何输出或引发任何错误,我想知道这可能是什么原因

我还想知道如何在代码中使用多处理,也许继承是个问题

class Skeleton:
    def __init__(self, path, **kwargs):

        if type(path) is str:
                self.path = path
                self.inputStack = loadStack(self.path).astype(bool)
        if kwargs != {}:
            aspectRatio = kwargs["aspectRatio"]
            self.inputStack = ndimage.interpolation.zoom(self.inputStack, zoom=aspectRatio, order=2, 
            prefilter=False)

    def setThinningOutput(self, mode="reflect"):
        # Thinning output
        self.skeletonStack = get_thinned(self.inputStack, mode)
    def setNetworkGraph(self, findSkeleton=False):
        # Network graph of the crowded region removed output
        self.skeletonStack = self.inputStack
        self.graph = get_networkx_graph_from_array(self.skeletonStack)

    def setPrunedSkeletonOutput(self):
        # Prune unnecessary segments in crowded regions removed skeleton
        self.setNetworkGraph(findSkeleton=True)
        self.outputStack = pr.getPrunedSkeleton(self.skeletonStack, self.graph)
        saveStack(self.outputStack, self.path + "pruned/")


class Trabeculae (Skeleton):
    pass

def TrabeculaeY (path):
     path_mrb01_square = Trabeculae(path)
     path_mrb01_square.setPrunedSkeletonOutput()

if __name__=='__main__':

    path1 = (r' ')
    path2 = (r' ')
    path3 = (r' ')
    the_list =[]
    the_list.append(path1)
    the_list.append(path2)
    the_list.append(path3)
    for i in range (0,len(the_list)):
        p1 = multiprocessing.Process(target=TrabeculaeY, args=(the_list[i],))
        p1.start()
        p1.join()

Tags: thepath代码selfifdefkwargslist
1条回答
网友
1楼 · 发布于 2024-10-03 21:35:16

继承对于多重处理来说不是问题

不能join()循环内的进程。这意味着循环要等到p1完成它的工作,然后才能继续下一个循环

相反,在循环中启动所有进程,然后在第二个循环中等待所有进程,如下所示:

if __name__=='__main__':

    path1 = (r' ')
    path2 = (r' ')
    path3 = (r' ')
    the_list =[]
    the_list.append(path1)
    the_list.append(path2)
    the_list.append(path3)
    started_processes = []
    for i in range (0,len(the_list)):
        p1 = multiprocessing.Process(target=TrabeculaeY, args=(the_list[i],))
        p1.start()
        started_processes.append(p1)
    for p in started_processes:
        p.join()  

我用于测试的完整代码:

import multiprocessing


class Skeleton:
    def __init__(self, path, **kwargs):
        self.path = path
        pass

    def setThinningOutput(self, mode="reflect"):
        pass

    def setNetworkGraph(self, findSkeleton=False):
        pass

    def setPrunedSkeletonOutput(self):
        print(self.path)


class Trabeculae(Skeleton):
    pass


def TrabeculaeY(path:str):
    path_mrb01_square = Trabeculae(path)
    path_mrb01_square.setPrunedSkeletonOutput()


if __name__ == '__main__':
    the_list = [r'1', r'2', r'3']
    started_processes = []
    for path in the_list:
        process = multiprocessing.Process(target=TrabeculaeY, args=path)
        process.start()
        started_processes.append(process)

    for process in started_processes:
        process.join()

相关问题 更多 >