The imap() function works in a somewhat similar fashion to your standard map function except for the fact that it returns an iterable. This can be very advantageous in certain situations, and I'm a big fan of using iterables, mainly, because Python features some very elegant ways to handle iterables.
In this example, we will call iterable = p.imap(), passing it in both myTask and list that we wish to map to our pool. Once this has finished, we then iterate over all of the results within this iterable using the next() method, and print this out to the console:
from multiprocessing import Pool
import time
def myTask(n):
time.sleep(n+2)
return n+2
def main():
with Pool(4) as p:
for iter in p.imap(myTask, [1,3,2,1]):
print(iter)
if __name__ == '__main__':
main()
Upon execution of this program, you should see every element of the array we've passed in being doubled and printed out on our console in the order that it was submitted to our pool in.
$ python3.6 17_mapPool.py
3
5
4
3