user337105 user337105 - 5 years ago 279
Python Question

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

Answer Source

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.

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=lambda retval: result.append(retval))
    pool.close()
    pool.join()
    print(result)
    # [0, 1, 4, 9, 16, ... 9409, 9604, 9801]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download