Reputation: 9078
What would be the best way to get the index of the current output of imap_unordered
in python multiprocessing
?
Upvotes: 1
Views: 376
Reputation: 69082
use enumerate()
on the sequence of arguments to your target function, and either change the function to return the index in addition to the result, or create a wrapper function that does that.
Simple example:
from multiprocessing import Pool
import time
import random
def func(args):
# real target function
time.sleep(random.random())
return args ** 2
def wrapper(args):
idx, args = args
return (idx, func(args))
if __name__ == '__main__':
pool = Pool(4)
args = range(10) # sample args
results = pool.imap_unordered(wrapper, enumerate(args))
for idx, result in results:
print(idx, result)
Upvotes: 4