This code seems to show the difference between a for loop and Pool, calling a method on different instances:
from multiprocessing import Pool
instances = ['a','ab','abc','abcd']
def calc_stuff(i):
return len(i)
if __name__ == '__main__':
print('One at a time')
for i in instances:
print(len(i))
print('Use Pool')
with Pool(4) as pool:
print(pool.map(calc_stuff, instances))
Note the use of if __name__ == '__main':
This separates each process out.
Output:
One at a time
1
2
3
4
Use Pool
[1, 2, 3, 4]