Pool map with multiple parameters in Python
multiprocessing
This article will explain different ways to perform parallel function execution
using the module in Python .
multiprocessing
The module provides functionality to perform parallel function execution using multiple inputs and distribute the input data among different processes.
We can execute functions with different input values in parallel by using the following method in Python.
Use pool.map()
the method to execute parallel functions
pool.map(function, iterable)
The method returns an iterator that applies the provided as input to each item of function
the input iterable
. So if we want to execute in parallel with different inputs 函数
, we can use pool.map()
the method.
The following sample code demonstrates how to use pool.map()
the method to parallelize function execution in Python.
from multiprocessing import Pool
def myfunc(x):
return 5 + x
if __name__ == "__main__":
with Pool(3) as p:
print(p.map(myfunc, [1, 2, 3]))
Output:
[6, 7, 8]
If the input function
has multiple parameters, we can use pool.map()
the method and partial()
function to execute the function in parallel.
The following example demonstrates how to use Python pool.map()
to execute functions in parallel with multiple arguments.
from multiprocessing import Pool
from functools import partial
def multiply(x, y):
print(x * y)
if __name__ == "__main__":
with Pool(3) as p:
p.map(partial(multiply, 5), [1, 2, 3])
Output:
5
10
15
As can be seen in the above example, the disadvantage of this approach is that we cannot change the value of the first parameter.
Use pool.starmap()
the method to execute parallel functions with multiple parameters
If we want to execute a function with multiple arguments in parallel, we can use pool.starmap(function, iterable)
the method.
Like pool.map(function, iterable)
the method, pool.starmap(function, iterable)
the method returns an iterator that function
applies the provided as input to iterable
each item of . However, it expects each input item of iterable
to be arranged as an input function
argument iterable.
By using pool.starmap()
the method, we can function
provide different values for all the parameters of , unlike pool.map()
the method.
We can pool.starmap()
perform parallel function execution with multiple arguments in Python using method in the following way.
from multiprocessing import Pool
def print_name(name, lname):
print("full name =", name, lname)
if __name__ == "__main__":
with Pool(3) as p:
p.starmap(print_name, [("Thomas", "Scott"), ("Ali", "Khan")])
Output:
full name = Thomas Scott
full name = Ali Khan
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Implementing a Low-Pass Filter in Python
Publish Date:2025/05/07 Views:89 Category:Python
-
Low pass filter is a term in signal processing basics and is often used to filter signals to obtain more accurate results. This tutorial will discuss the low-pass filter and how to create and implement it in Python. A low-pass filter is use
Implementing Curl command in Python using requests module
Publish Date:2025/05/07 Views:97 Category:Python
-
requests This article will discuss and implement different curl commands using the module in Python . requests Installing modules in Python Python provides us with requests the module to execute curl command. Install it in Python 3 using Pi
Using fetchall() in Python to extract elements from a database
Publish Date:2025/05/07 Views:171 Category:Python
-
This article aims to describe fetchall() the working methods of extracting elements from a database using and how to display them correctly. This article will also discuss list(cursor) how functions can be used in programs. fetchall() Extra
Parsing log files in Python
Publish Date:2025/05/07 Views:106 Category:Python
-
Log files contain information about events that occurred during the operation of a software system or application. These events include errors, requests made by users, bugs, etc. Developers can further scan these usage details to find poten
Declaring a variable without a value in Python
Publish Date:2025/05/07 Views:57 Category:Python
-
A variable is a reserved memory location that can store some value. In other words, variables in a Python program provide data to the computer to process operations. Every value in Python has a data type. There are numbers, lists, tuples, e
Defining class global variables in Python
Publish Date:2025/05/07 Views:81 Category:Python
-
A global variable is a variable that is visible and available in every part of the program. Global variables are also not defined in any function or method. On the other hand, local variables are defined in functions and can be used only in
Incrementing loop step by 2 in Python
Publish Date:2025/05/07 Views:199 Category:Python
-
In each iteration, for the loop increases the counter variable by a constant. A loop with the sequence 0, 2, 4, 6 for will increase the counter variable by 2 each iteration. This article will show you some for ways to increment by 2 in a lo
Pretty Printing Dictionaries in Python
Publish Date:2025/05/07 Views:126 Category:Python
-
This tutorial will show you how to pretty print dictionaries in Python. Pretty printing means presenting some printed content in a more readable format or style. pprint() Pretty printing dictionaries in Python pprint is a Python module that
Writing logs to a file in Python
Publish Date:2025/05/06 Views:133 Category:Python
-
This tutorial will show you how to write logs to files in Python. Use the module in Python logging to write logs to files Logging is used to debug a program and find out what went wrong. logging The log module is used to log data to a file