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

后端 未结 5 850
夕颜
夕颜 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条回答
  •  萌比男神i
    2021-01-01 20:03

    If you don't absolutely need to stick with Multiprocessing module then, it can easily achieved using concurrents.futures library

    here's the example code:

    from concurrent.futures.thread import ThreadPoolExecutor, wait
    
    MAX_WORKERS = 20
    
    class MyClass():
        def __init__(self, input):
            self.input = input
            self.result = int
    
        def my_process(self, multiply_by, add_to):
            self.result = self.input * multiply_by
            self._my_sub_process(add_to)
            return self.result
    
        def _my_sub_process(self, add_to):
            self.result += add_to
    
    list_of_numbers = range(0, 5)
    list_of_objects = [MyClass(i) for i in list_of_numbers]
    
    With ThreadPoolExecutor(MAX_WORKERS) as executor:
        for obj in list_of_objects:
            executor.submit(obj.my_process, 100, 1).add_done_callback(on_finish)
    
    def on_finish(future):
        result = future.result() # do stuff with your result
    

    here executor returns future for every task it submits. keep in mind that if you use add_done_callback() finished task from thread returns to the main thread (which would block your main thread) if you really want true parallelism then you should wait for future objects separately. here's the code snippet for that.

    futures = []
    with ThreadPoolExecutor(MAX_WORKERS) as executor:
        for objin list_of_objects:
            futures.append(executor.submit(obj.my_process, 100, 1))
    wait(futures)
    
    for succeded, failed in futures:
        # work with your result here
        if succeded:
           print (succeeeded.result())
        if failed:
            print (failed.result())
    

    hope this helps.

提交回复
热议问题