Using multiprocessing.Pool in a class on Windows with IPython Notebook

I wrote a class method to create multiple instances of a second class using multiprocessing.Pool(see below). Using IPython, this works on Linux / OSX, but not on Windows (it will hang). So my question is; Is it possible, and what is the shortest way, to change my code so that it works on Windows? (Editing: this is apparently an issue specific to IPython laptop)

I read numerous reports about problems with Windows that are lacking fork()and necessary if __name__ == "__main__":, but I do not understand how to implement this in this case if I use IPython Notebook.

Thank you, Chris.

my_class_factory.py

import multiprocessing

class MyClass(object):
    def __init__(self, a=1):
        self.a = a

def unpack_and_make_class(val_dict):
    if val_dict.has_key('args'):
        args = val_dict.pop('args')
    else:
        args = []            
    return MyClass(*args, **val_dict)

class MyClassFactory(object):

    def make_classes(self, inputs):

        pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
        mapping = pool.map

        myclasses = mapping(unpack_and_make_class, inputs)

        pool.close()
        pool.join()

        return myclasses

then in ipython laptop:

In [1]: from my_class_factory import MyClassFactory
In [2]: mcf = MyClassFactory()
In []: cls = mcf.make_classes([{'a':1}, {'a':2}, {'a':3}])

Linux/OSx, Windows :

AssertionError: __main__
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\chris\Anaconda\lib\multiprocessing\forking.py", line 380, in main
    prepare(preparation_data)
  File "C:\Users\chris\Anaconda\lib\multiprocessing\forking.py", line 488, in prepare
    assert main_name not in sys.modules, main_name
+4

All Articles