import pandas as pd from IPython.parallel import Client, error class Load_balanced_view(object): def __init__(self, profile='default'): #initialise a direct and a load ballanced view self.c = Client(profile=profile) self.dview = self.c[:] self.lbview = self.c.load_balanced_view() def exec_on_engine(self, code, block=True): """ Execute the given code on all engines Parameters ---------- to_execute: string or list of strings command(s) to execute on all the nodes. Thought for short tasks, like importing modules block: bool whether or not to wait until done to return. default: True """ #Six: Python 2 and 3 Compatibility Library from six import string_types #appropriate string type if isinstance(code, string_types): # if it's a string code = [code,] # convert to list # execute the required commands # (better to do in block mode, avoids errors if command is slow) for te in code: try: self.dview.execute(te, block=block) except error.CompositeError as e: # if an error occurs, print a single one, not one per engine e.raise_exception() parallel_env = Load_balanced_view(profile='default') #parallel_env = Load_balanced_view(profile='empty') imports = ['import numpy as np',] parallel_env.exec_on_engine(imports) imports = ['import numpy as np',] parallel_env.exec_on_engine(imports) imports = ['import pandas as pd',] parallel_env.exec_on_engine(imports) imports = ['import pandas as pd',] parallel_env.exec_on_engine(imports)