amiref
amiref

Reputation: 3441

How to multi-thread these two nested loops?

I have two loops iterating over different lists. How can I write a multi-thread python program in a way that all the iterations of inner loop for each element of outer loop happen in different threads? For example we have two following lists:

A = [1,2,3]
B = [4,5,6]
C = []
for i in A:
   for j in B:
      C.append(i+j)

Here, we have nine iterations in total. Every time that the first iterator is on one of the A's elements, the other iterator, iterates over all the elements of B. And, this operation repeats three times. I want to know how I can put these three operations in three separated threads.

Upvotes: 2

Views: 415

Answers (1)

Amos
Amos

Reputation: 3276

I'd suggest using Numba's prange for these kind of tasks. http://numba.pydata.org/

Vanilla CPython doesn't handle parallel processing well due to GIL.

import numba

@numba.njit(parallel=True)
def work():
    A = [1,2,3]
    B = [4,5,6]
    X = [[0 for x in range(0)] for _ in A]
    for idx in numba.prange(len(A)):
       i = A[idx]
       for j in B:
          X[idx].append(i+j)
    C = [0 for x in range(0)]
    for x in X:
       C = C + x
    print(C)

work()

Upvotes: 1

Related Questions