Skip to content Skip to sidebar Skip to footer

How To Run Generator Code In Parallel?

I have code like this: def generator(): while True: # do slow calculation yield x I would like to move the slow calculation to separate process(es). I'm workin

Solution 1:

In this type of situation I usually use the joblib library. It is a parallel computation framework based on multiprocessing. It supports memmapping precisely for the cases where you have to handle large numpy arrays. I believe it is worth checking for you.

Maybe joblib's documentation is not explicit enough on this point, showing only examples with for loops, since you want to use a generator I should point out that it indeed works with generators. An example that would achieve what you want is the following:

from joblib import Parallel, delayed
defmy_long_running_job(x):
    # do something with x# you can customize the number of jobs
Parallel(n_jobs=4)(delayed(my_long_running_job)(x) for x in generator())

Edit: I don't know what kind of processing you want to do, but if it releases the GIL you could also consider using threads. This way you won't have the problem of having to transfer large numpy arrays between processes, and still beneficiate from true parallelism.

Post a Comment for "How To Run Generator Code In Parallel?"