Apply a method to a list of objects in parallel using multi-processing

后端 未结 5 839
夕颜
夕颜 2021-01-01 18:58

I have created a class with a number of methods. One of the methods is very time consuming, my_process, and I\'d like to do that method in parallel. I came acro

5条回答
  •  离开以前
    2021-01-01 19:44

    If your class is not "huge", I think process oriented is better. Pool in multiprocessing is suggested.
    This is the tutorial -> https://docs.python.org/2/library/multiprocessing.html#using-a-pool-of-workers

    Then seperate the add_to from my_process since they are quick and you can wait util the end of the last process.

    def my_process(input, multiby):
        return xxxx
    def add_to(result,a_list):
        xxx
    p = Pool(5)
    res = []
    for i in range(10):
        res.append(p.apply_async(my_process, (i,5)))
    p.join()  # wait for the end of the last process
    for i in range(10):
        print res[i].get()
    

提交回复
热议问题