Search code examples
pythonmultithreadingpython-multithreadingpool

How to append return value of function into list in Pool?


I'm curious if it is possible to append a return value from function f(x,y) into, for example, list.

So I have this:

Parallel:

 def download_unit_LVs(self, cislo_uzemia):
     pool = Pool(number_of_workers)
     for cislo in cisla:
         pool.apply_async(self.download_cislo, args=(cislo_uzemia,cislo))
     pool.close()
     pool.join()
     self.manager.commit()

This is the way how I run the method self.download_cislo parallel but the problem is, that it returns a value which I have to append to results list.

How to do that?

Sequential:

 def download_unit_LVs(self, cislo_uzemia):
        results = []
        for cislo in cisla:
            results.append(self.download_cislo(cislo_uzemia,cislo))
        self.manager.commit()

Solution

  • The pool.apply_async call can be passed a callback function. The callback function will be called when foo function completes and will be passed the value returned by foo. Note that the return value must be picklable, as the communication between processes is done with a Queue.

    import multiprocessing as mp
    
    def foo(x):
        return x * x
    
    if __name__ == '__main__':
        result = []
        pool = mp.Pool()
        for i in range(100):
            pool.apply_async(foo, args=(i, ), callback=result.append)
        pool.close()
        pool.join()
        print(result)
        # [0, 1, 4, 9, 16, ... 9409, 9604, 9801]