2

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)

3 Answers 3

2

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))

Sign up to request clarification or add additional context in comments.

Comments

1

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!

Comments

1

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.