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()
self.download_cislo
def download_unit_LVs(self, cislo_uzemia):
results = []
for cislo in cisla:
results.append(self.download_cislo(cislo_uzemia,cislo))
self.manager.commit()
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]