{python}series.map(func) works the same as the builtin python map, but it returns immediately a series.
multiprocessing
Implementation
Parallelised code:
If the results can be computed from partial results from partial inputs, the problem is called embarrassingly parallel
Notice that we added the {python}if __name__ == "__main__":statement. If we didn't do that, each child process would load the file again, and infinitely spawn processes. Yes multiprocessing is weird
from multiprocessing import Pool
# Function to compute the square of a number
def compute_square(num):
return num * num
# List of numbers to compute squares for
numbers = [1, 2, 3, 4, 5]
def run(numbers):
# Create a Pool object with 4 processes
with Pool(processes=4) as pool:
# Map the function to the list of numbers
results = pool.map(compute_square, numbers)
print("Input Numbers: ", numbers)
print("Squared Numbers: ", results)
if __name__ == "__main__":
run(numbers)
Most of the cases you then only need to combine the results somehow.
Variables created within processes are not shared. They get destroyed after the process has finished executing. This is why we require special variables like pool or queues from the multiprocessing library.
Which has the obvious difference of each map being potentially run on a different process.
Alternatives
List comprehensions. If in doubt, list comprehensions are most likely the better choice in python.