Reputation: 413
I have the below code and I want to get the sum of each list in the nested listed parallelly using multi-processing or threading or any other method in Python 3.x. How do I approach this? All the sub lists or atleast 2 sub lists should run the addition
function at the same time.
Thanks in advance!
nested_list = [[2,3,4], [4,5,6], [7,8,9], [10,11,12]]
def addition(list):
tot = sum(list)
return tot
for list in nested_list:
tot = addition(list)
print (tot)
Upvotes: 2
Views: 1323
Reputation: 65
multiprocessing module can be used for process based parallel programming.
from multiprocessing import Pool
nested_list = [[2,3,4], [4,5,6], [7,8,9], [10,11,12]]
with Pool(5) as p:
print(p.map(sum, nested_list))
Upvotes: 1
Reputation: 405
You don't need threads, because your task is CPU bound.
from multiprocessing import Pool
with Pool(5) as p:
print(p.map(addition, nested_list))
Other way
import concurrent
with concurrent.futures.ProcessPoolExecutor() as executor:
print(executor.map(addition, nested_list))
Upvotes: 2
Reputation: 4064
Give a woman or a man a fish and you feed her or him for a day. Teach woman or man to fish and you feed her or him for a lifetime.
Check this article. It's awesome! The case is not of a complicated matter, so you'll be ready to solve this and similar problems after 10-15 minutes of reading and trying.
Note! You wouldn't want to look toward multithreading
in a case of CPU-bound task (such as yours). In this case, in case of Python, only multiprocessing will introduce parallelism.
Good luck!
Upvotes: 1