How to run an ipython notebook in parallel just ch

2019-04-10 20:02发布

问题:

I have an IPython notebook with multiple cells that I want to run several times in parallel with only a difference on some parameters (which are defined inside on of the notebook's cells). What is the easiest way of doing it?

I have a workstation with 12 cores. whenever I run a notebook it only uses one of the cores. I want to use the other cores to run the exact same notebook but with some modifications of some parameters. Is it possible?

Thank you very much

回答1:

I actually then found a way to do it.

import multiprocessing
list_of_variables=something #a 2D numpy array with all your combinations of variable values, 
             #each row has a different combination of parameter values
number_of_combinations=shape(list_of_variables)[0] #number of rows
def notebook(i):
    variables=list_of_variables[i,:]
    #all your code here
pool = multiprocessing.Pool(processes=n)  # Create a pool with n workers.
pool.map(notebook,range(number_of_combinations)) 
pool.close()
pool.join()

in the code above notebook is a python function that receives as input a number, get the variables values for this iteration and runs every original cell.