How to parallelize a for-loop within a defined function using Ray in Python

I have been tasked with parallelizing a piece of code for a huge project I am taking a part in. The code.py I have been tasked with the parallelization goes something like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import numpy as np #importing modules sections
from tqdm import tqdm
import time
import os
from Auxiliar_py.functions import func1 #import func1 from functions.py within Auxiliar_py folder
... #many lines of code omitted for conciseness
constant1=10 #constant section
constant2=20
loops=fits.open('loops.fits')
loops_list=len(loops)
...
def mainFunction(arg1,arg2,arg3,...): #the main function
var01=constant1+1
var02=func1(var01)
...
attempt=0
max_attempts=5
for i in tqdm(range(len(loops))):
while attempt < max_attempts:
try:
for p in range(5): # <------ the loops I want to parallelize
var11=func1(var01) # <------
var12=func1(var11) # <------
var13=func2(var01,var12) # <------
... # <------ this entire sections takes 400+ lines of code
break
except Exception as e:
print(f"An error occurred: {e}")
attempt += 1
print(f"Attempt {attempt} out of {max_attempts}")
var21=var11+20
var22=func3(var13)
....
</code>
<code>import numpy as np #importing modules sections from tqdm import tqdm import time import os from Auxiliar_py.functions import func1 #import func1 from functions.py within Auxiliar_py folder ... #many lines of code omitted for conciseness constant1=10 #constant section constant2=20 loops=fits.open('loops.fits') loops_list=len(loops) ... def mainFunction(arg1,arg2,arg3,...): #the main function var01=constant1+1 var02=func1(var01) ... attempt=0 max_attempts=5 for i in tqdm(range(len(loops))): while attempt < max_attempts: try: for p in range(5): # <------ the loops I want to parallelize var11=func1(var01) # <------ var12=func1(var11) # <------ var13=func2(var01,var12) # <------ ... # <------ this entire sections takes 400+ lines of code break except Exception as e: print(f"An error occurred: {e}") attempt += 1 print(f"Attempt {attempt} out of {max_attempts}") var21=var11+20 var22=func3(var13) .... </code>
import numpy as np #importing modules sections
from tqdm import tqdm
import time
import os
from Auxiliar_py.functions import func1 #import func1 from functions.py within Auxiliar_py folder
... #many lines of code omitted for conciseness

constant1=10 #constant section
constant2=20
loops=fits.open('loops.fits')
loops_list=len(loops)
...

def mainFunction(arg1,arg2,arg3,...): #the main function
    var01=constant1+1
    var02=func1(var01)
    ...
    attempt=0
    max_attempts=5
    for i in tqdm(range(len(loops))):
         while attempt < max_attempts:
                try:

                    for p in range(5):           # <------ the loops I want to parallelize 
                        var11=func1(var01)       # <------ 
                        var12=func1(var11)       # <------ 
                        var13=func2(var01,var12) # <------ 
                        ...                      # <------ this entire sections takes 400+ lines of code
              

                    break  
                except Exception as e:
                    print(f"An error occurred: {e}")
                    attempt += 1
                    print(f"Attempt {attempt} out of {max_attempts}")
    var21=var11+20
    var22=func3(var13)
    ....


To my horror, there are nested loops within the desired function which takes a HUGE toll on the performance. While I am able to, using Ray, parallelize other sections of the code including some sections within for p in range(5), the entire section of the loop bugs me the most. Is there a way to parallelize that for-loop using Ray without rewriting 400+ lines of code within it?

The solutions I could think of but too scared to attempt are:

  1. Turn all of the functions and processes within the loop into 1 function, decorate it with @ray.remote, and use it here at the loops to enable parellization. This will, however, require me to manually list the inputs and outputs of the 400+ codes so I can define it as a function.

  2. Rewrite everything.

I’m currently new to parallelization so any answers and comments are hugely appreciated. Thank you all in advance.

Note: While other packages/modules for parallelization are avalible, Ray is the most suitable for this project as a whole. It will be greatly appreciated if I can stick to Ray and not change to other packages.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật