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

后端 未结 5 848
夕颜
夕颜 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:56

    Based on the answer of Python Multiprocessing - apply class method to a list of objects and your code:

    1. add MyClass object into simulation object

      class simulation(multiprocessing.Process):
          def __init__(self, id, worker, *args, **kwargs):
              # must call this before anything else
              multiprocessing.Process.__init__(self)
              self.id = id
              self.worker = worker
              self.args = args
              self.kwargs = kwargs
              sys.stdout.write('[%d] created\n' % (self.id))
      
    2. run what you want in run function

          def run(self):
              sys.stdout.write('[%d] running ...  process id: %s\n' % (self.id, os.getpid()))
              self.worker.my_process(*self.args, **self.kwargs)
              sys.stdout.write('[%d] completed\n' % (self.id))
      

    Try this:

    list_of_numbers = range(0, 5)
    list_of_objects = [MyClass(i) for i in list_of_numbers]
    list_of_sim = [simulation(id=k, worker=obj, multiply_by=100*k, add_to=10*k) \
        for k, obj in enumerate(list_of_objects)]  
    
    for sim in list_of_sim:
        sim.start()
    

提交回复
热议问题