A simple way of implementing multiprocessing in python is
from multiprocessing import Pool
def calculate(number):
return number
if __name__ == '__main__':
pool = Pool()
result = pool.map(calculate, range(4))
An alternative implementation based on futures is
from concurrent.futures import ProcessPoolExecutor
def calculate(number):
return number
with ProcessPoolExecutor() as executor:
result = executor.map(calculate, range(4))
Both alternatives do essentially the same thing, but one striking difference is that we don't have to guard the code with the usual if __name__ == '__main__'
clause. Is this because the implementation of futures takes care of this or us there a different reason?
More broadly, what are the differences between multiprocessing
and concurrent.futures
? When is one preferred over the other?
EDIT:
My initial assumption that the guard if __name__ == '__main__'
is only necessary for multiprocessing was wrong. Apparently, one needs this guard for both implementations on windows, while it is not necessary on unix systems.