I need to parallelize a Python for loop in which for every iteration, a function (that takes two arguments) is called that returns two results, and then these results are appended to two different lists. The for loop iterates over two lists of arguments.
So say I have the following code:
def my_f(a, b):
res1 = a + b
res2 = a * b
return res1, res2
# lists of arguments
args1 = [1, 2, 3, 4]
args2 = [5, 6, 7, 8]
res_list1, res_list2 = [], []
for i in range(len(args1)): # loop to parallelize
res1, res2 = my_f(args1[i], args2[i])
res_list1.append(res1)
res_list2.append(res2)
The result should be
res_list1 = [6, 8, 10, 12]
res_list2 = [5, 12, 21, 32]
How would I go about making it run in parallel?
I am aware that in C/C++ one can just use #pragma omp for to obtain a parallel for. Is there anything similar in Python?
I am using python 3.8.5 on Linux, but I need to have it work on any OS.