简体   繁体   中英

Implementing multiprocessing for a nested loop calculation (python)

I am trying to implement multiprocessing to speed up a nested loop. Following is a psudo code of what happens:

result = []
z = []
a1 = array of values
a2 = array of values
for i in range(len(a1)):
    x = a1[i]
        for j in range(len(a2)):
            y = a2[j]
            z.append(function(x,y))
    result.append(max(z))
plot(a1,result)

I am a novice python user. The problem that I faced previously when trying to follow a tutorial was when implementing multiprocessing.Proceses, the code runs the function block but doesn't return the results array (or at least I don't know how to make it return that array). The second loop (a2) is the one that takes the most time, I was hoping someone could guide me as to how I can improve the calculation speed. Thank you

This kind of problem is exactly why the newconcurrent.futures module was created.

You can define your work function and then map it to an array of inputs via an executor with multiprocessing (or multithreading if better suited).

See the examples in there, especially the ProcessPoolExecutor Example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM