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 ,

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.