0

how can i control the return value of this function pool apply_asyn supposing that I have the following cool

import multiprocessing:


de fun(..)
...
...
return value


my_pool = multiprocessing.Pool(2)


for i in range(5) :
    result=my_pool.apply_async(fun, [i])

 some code going to be here....

digest_pool.close()
digest_pool.join()
here i need to proccess the results 

how can i control the result value for every proccess and know to check to which proccess it belongs ,

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
AKM
  • 6,285
  • 9
  • 31
  • 34

2 Answers2

0

Are you sure, that you need to know, which of your two workers is doing what right now? In such a case you might be better off with Processes and Queues, because, this sounds as some communication between the multiple processes is required.

If you just want to know, which result was processed by which worker, you can simply return a tuple:

#!/usr/bin/python
import multiprocessing

def fun(..)
...
    return value, multiprocessing.current_process()._name

my_pool = multiprocessing.Pool(2)
async_result = []

for i in range(5):
    async_result.append(my_pool.apply_async(fun, [i]))

# some code going to be here....

my_pool.join()
result = {}
for i in range(5):
    result[i] = async_result[i].get()

If you have the different input variables as a list, the map_async command might be a better decision:

#!/usr/bin/python
import multiprocessing

def fun(..)
...
...
    return value, multiprocessing.current_process()._name

my_pool = multiprocessing.Pool()

async_results = my_pool.map_async(fun, range(5))

# some code going to be here....

results = async_results.get()

The last line joins the pool. Note, that results is a list of tuples, each tuple containing of your calculated value and the name of the process who calculated it.

Sebastian Werk
  • 1,568
  • 2
  • 17
  • 30
0

store the the value of 'i' from the for loop and either print it or return and save it somewhere else. so if a process happens you can check from which process it was by looking at the variable i.

Hope this helps.

Pavan
  • 17,840
  • 8
  • 59
  • 100