Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

joblib parallel processing of a multiple return values function

I use joblib to parallelise a function (with multiprocessing). But, this function return 4 values but when I get the results from Parallel it gives me only 3 values

from joblib import Parallel, delayed 
import numpy as np
from array import array
import time

def best_power_strategy():
    powerLoc = {0}
    speedLoc = {1}
    timeLoc = {2}
    previousSpeedLoc = {3}        
    return powerLoc,speedLoc,timeLoc,previousSpeedLoc

if __name__ == "__main__":
    realRiderName=['Rider 1', 'Rider 2', 'Rider 3']
    powerLoc = {}
    speedLoc = {}
    timeLoc = {}
    previousSpeedLoc = {}
    powerLoc,speedLoc,timeLoc,previousSpeedLoc = Parallel(n_jobs=3)(delayed(best_power_strategy)() for rider in realRiderName)
    print(powerLoc)
    print(speedLoc)
    print(timeLoc)
    print(previousSpeedLoc)

and the result is :

ValueError: not enough values to unpack (expected 4, got 3)

Does someone has an idea?

Thanks in advance

like image 676
Doctor Pi Avatar asked Jul 30 '18 16:07

Doctor Pi


People also ask

Does Joblib use multiprocessing?

There are multiple backends in joblib, which means using different ways to do the parallel computing. If you set the backend as multiprocessing, under the hood, it is actually create a multiprocessing pool that uses separate Python woker processes to execute tasks concurrently on separate CPUs.

Is Joblib faster than multiprocessing?

Pool in some cases #1108.

How do I use Joblib parallel in Python?

With the Parallel and delayed functions from Joblib, we can simply configure a parallel run of the my_fun() function. n_jobs is the number of parallel jobs, and we set it to be 2 here. i is the input parameter of my_fun() function, and we'd like to run 10 iterations.

Does Joblib parallel preserve order?

TL;DR - it preserves order for both backends.


1 Answers

If you want to store the results in four separate names, you can zip the results from your generator together and then expand them into the desired names, i.e.:

# shortening the names for simplicity/readability
riders = ["Rider 1", "Rider 2", "Rider 3"]
p, s, t, pv = zip(*Parallel(n_jobs=3)(delayed(best_power_strategy)() for r in riders))

This will result in p containing all the powerLoc results, s containing all the speedLoc results and so on...

Now, given that your best_power_strategy function is essentially static and nothing changes (you're not even sending a rider to it), this piece of code is pretty useless as you'll always have the same results, but I take it you're using this just as an example.

like image 140
zwer Avatar answered Sep 20 '22 22:09

zwer