The imap_unordered is aptly named as such, because when used, it returns an iterable map, and it executes the tasks submitted to it in an unordered fashion:
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_unordered(myTask, [1,3,2,1]):
print(iter)
if __name__ == '__main__':
main()
Upon execution of this preceding program, you should see something like this:
$ python3.6 17_mapPool.py
3
3
4
5
We've been returned an iterable that contains transformed elements, which are not in the same order as they were inputted. imap_unordered has returned the elements in the order in which they completed.