sermakarevich / jigsaw-toxic-comment-classification-challenge

46 stars 8 forks source link

parallelize_dataframe unable to resolve passed function #2

Open ChrisPalmerNZ opened 6 years ago

ChrisPalmerNZ commented 6 years ago

Running on Windows 10. I am adding the code from your original Jupyter notebook into my own, and the only other change is to redefine paths on the notebook and config.py for my own situation.

When train = parallelize_dataframe(train, process_comment) is run then the code hangs indefinitely. In the jupyter notebook console I can see that parallelize_dataframe cannot actually resolve the passed function, so nothing is actually happening:

AttributeError: Can't get attribute 'process_comment' on <module 'main' (built-in)>

Could this be a peculiarity of my environment?

UPDATE - Definitely a peculiarity of my environment: Is it possible to define parallelize_dataframe so it copes with a Windows environment (see below)?

See: https://stackoverflow.com/questions/37103243/multiprocessing-pool-in-jupyter-notebook-works-on-linux-but-not-windows https://docs.python.org/2/library/multiprocessing.html#windows

Resolved by wrapping if __name__ == '__main__'

def parallelize_dataframe(df, func):
    if __name__ == '__main__':
        df_split = np.array_split(df, multiprocessing.cpu_count())
        pool = Pool(multiprocessing.cpu_count())
        df = pd.concat(pool.map(func, df_split))
        pool.close()
        pool.join()
    return df 
sermakarevich commented 6 years ago

I`ve also faced same problems with Pool on Windows. On Linux/Mac everything works fine. Pull request is welcome.