I read and re-read the documentation / tutorial on IPython, and I cannot understand the problem with this particular piece of code. It appears that the dimensionless_run function is not visible in the namespace delivered for each of the engines, but I got confused because the function is defined in __main__ and is clearly visible as part of the global namespace.
wrapper.py:
import math, os def dimensionless_run(inputs): output_file = open(inputs['fn'],'w') ... return output_stats def parallel_run(inputs): import math, os ## Removing this line causes a NameError: global name 'math' ## is not defined. folder = inputs['folder'] zfill_amt = int(math.floor(math.log10(inputs['num_iters']))) for i in range(inputs['num_iters']): run_num_str = str(i).zfill(zfill_amt) if not os.path.exists(folder + '/'): os.mkdir(folder) dimensionless_run(inputs) return if __name__ == "__main__": inputs = [input1,input2,...] client = Client() lbview = client.load_balanced_view() lbview.block = True for x in sorted(globals().items()): print x lbview.map(parallel_run,inputs)
Running this code after ipcluster start --n=6 yields a sorted global dictionary, including the math and os modules, and the parallel_run and dimensionless_run functions. This is followed by IPython.parallel.error.CompositeError: one or more exceptions to the method call: parallel_run, which consists of a large number of [n:apply]: NameError: global name 'dimensionless_run' is not defined , where n runs from 0-5 .
There are two things that I don’t understand, and they are clearly related.
- Why doesn't the code identify
dimensionless_run in the global namespace? - Why is
import math, os required in the parallel_run definition?
Edited by:. This turned out to be not a very big namespace error - I ran ipcluster start --n=6 in a directory that did not contain code. To fix this, all I had to do was execute the start command in my code directory. I also fixed this by adding the lines:
inputs = input_pairs os.system("ipcluster start -n 6") #NEW client = Client() ... lbview.map(parallel_run,inputs) os.system("ipcluster stop") #NEW
who started the required cluster in the right place.
source share