logo-MindMajix
PostPythonPython
S
Satish santu

Posted on 19th July 2024|73 views

0
votes

Python Parallel For Loop

How can I parallelize a python for loop?

Here is my code, but is it possible to parallelize this loop in python

result1 = list() 

result2 = list() 

result3 = list() 

for i in range(0, 10):

 parameter = i * offset 

 res1, res2, res3 = calc_stuff(parameter = parameter) 

 result1.append(res1) 

 result2.append(res2)

 result3.append(res3)

 

1 answers
Answers
P
jordan chris

Posted on 19th July 2024| views

You can use ray have a look here

import ray 

ray.init() 

@ray.remote(num_return_vals=3) 

def calc_stuff(parameter=None):

 #some operations. 

return 1, 2, 3

Now invoke this ray in the program like this

result1, result2, result3 = [], [], [] 

for j in range(10): 

res1, res2, res3 = calc_stuff.remote(parameter=j) 

result1.append(res1) 

result2.append(res2) 

result3.append(res3) 

result1 = ray.get(result1) 

result2 = ray.get(result2) 

result3 = ray.get(result3)

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo

  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos
Name
Course *
Email *
Phone Number