简体   繁体   English

如何使用 inheritance 进行多处理?

[英]How to use multiprocessing with inheritance?

I'm trying to speed up the running time of the script with multiprocessing.我正在尝试通过多处理来加快脚本的运行时间。 When I've tried the same multiprocessing code with more simple definitions like resizing images on different directories the multiprocessing works well but when I tried it with the code seen below, it runs but it does not give any output or raise any errors and I was wondering what could be the reason for this.当我尝试使用更简单的定义(例如在不同目录上调整图像大小)的相同多处理代码时,多处理运行良好,但是当我使用下面看到的代码尝试它时,它运行但它没有给出任何 output 或引发任何错误,我是想知道这可能是什么原因。

I was also wondering how could I use multiprocessing with this code, maybe inheritance is the problem?我还想知道如何使用这段代码进行多处理,也许 inheritance 是问题所在?

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()

Inheritance is not a problem for multiprocessing. Inheritance 对于多处理来说不是问题。

You must not join() the processes inside the loop.您不能join()循环内的进程。 It means that the loop waits until p1 finished doing its work, before it continues with the next one.这意味着循环一直等到p1完成它的工作,然后再继续下一个。

Instead, start all processes in a loop, then wait for all processes in a second loop like this:相反,在一个循环中启动所有进程,然后在第二个循环中等待所有进程,如下所示:

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()  

Full code I used for testing:我用于测试的完整代码:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM