Receive a list of the results of parallel processing in Python with starmap

Introduction

--Speaking of python parallel processing, the multiprocess module --Use map when you want the result in a list ――But it seems that you can't pass multiple arguments in map ――For numerical experiments such as machine learning, I often experiment with different arguments, so one is a little. .. --In Python 2.X, in order to pass multiple arguments to map, it was necessary to define a wrapper function and write it to be passed, but since Python 3.3, it is easier to write with starmap. --This is my memo on how to use starmap.

Sample code

#!/usr/bin/env python3
from itertools import product
from multiprocessing import Pool

#Functions you want to process in parallel
def multi_test(arg1, arg2, arg3):
    return(arg1+arg2+arg3)

#Decide the number of parallels and prepare Pool
pool = Pool(3)

#Prepare argument variations with a list of tuples
multi_args = []
for iter1, iter2, iter3 in product(range(1,4), range(1,4), range(1,4)):    
    multi_args.append((iter1,iter2,iter3))
print(multi_args)    
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]

#Parallel processing execution
results = pool.starmap(multi_test, multi_args)  
print(results)
[3, 4, 5, 4, 5, 6, 5, 6, 7, 4, 5, 6, 5, 6, 7, 6, 7, 8, 5, 6, 7, 6, 7, 8, 7, 8, 9]

in conclusion

――I was able to write it simply. I've just started using it, so please tell me what's wrong.

reference

Recommended Posts

Receive a list of the results of parallel processing in Python with starmap
How to get a list of files in the same directory with python
Get the number of specific elements in a python list
Get the value of a specific key in a list from the dictionary type in the list with Python
How to identify the element with the smallest number of characters in a Python list?
Display a list of alphabets in Python 3
The story of blackjack A processing (python)
A function that measures the processing time of a method in python
Get a list of files in a folder with python without a path
[Python] Get the files in a folder with Python
Get the caller of a function in Python
Get a list of packages installed in your current environment with python
View the result of geometry processing in Python
Generate a list packed with the number of days in the current month.
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Output in the form of a python array
Parallel processing with no deep meaning in Python
How to pass the execution result of a shell command in a list in Python
plot the coordinates of the processing (python) list and specify the number of times in draw ()
[python] Get the list of classes defined in the module
[Python] Outputs all combinations of elements in the list
Group by consecutive elements of a list in Python
A reminder about the implementation of recommendations in Python
Parallel processing with Parallel of scikit-learn
How to check in Python if one of the elements of a list is in another list
How to count the number of occurrences of each element in the list in Python with weight
Try scraping the data of COVID-19 in Tokyo with Python
Visualize the results of decision trees performed with Python scikit-learn
Create a list in Python with all followers on twitter
Calculate the square root of 2 in millions of digits with python
Get a list of purchased DMM eBooks with Python + Selenium
[Note] Import of a file in the parent directory in Python
[Homology] Count the number of holes in data with Python
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Find the eigenvalues of a real symmetric matrix in Python
[Python] I examined the practice of asynchronous processing that can be executed in parallel with the main thread (multiprocessing, asyncio).
To receive multiple return values ​​from a function executed using parallel processing (Pool) in Python
[python] Manage functions in a list
[Python] Easy parallel processing with Joblib
Get the value of a specific key up to the specified index in the dictionary list in Python
Read files in parallel with Python
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
About the basics list of Python basics
How to pass the execution result of a shell command in a list in Python (non-blocking version)
Process the contents of the file in order with a shell script
How to determine the existence of a selenium element in Python
Save the result of the life game as a gif with python
Try to get a list of breaking news threads in Python.
How to check the memory size of a variable in Python
If you give a list with the default argument of the function ...
Output the contents of ~ .xlsx in the folder to HTML with Python
Read the standard output of a subprocess line by line in Python
I tried to create a list of prime numbers with python
The story of making a standard driver for db with python.
How to check the memory size of a dictionary in Python
[python] Get the rank of the values in List in ascending / descending order
Read a file in Python with a relative path from the program
Dockerfile with the necessary libraries for natural language processing in python
The idea of feeding the config file with a python file instead of yaml
Solve the subset sum problem with a full search in Python