Home > Software engineering >  Multiprocessing example giving AttributeError on Mac
Multiprocessing example giving AttributeError on Mac

Time:10-09

Recently I reinstalled the latest Anaconda3 on Mac and found some an error in

from multiprocessing import Pool
def f(x):
    return x*x

with Pool(5) as p:
    p.apply(f, [1])

The error is like

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

But the same code worked several days before!!!

CodePudding user response:

Try this:

from multiprocessing import Pool
def f(x):
    return x*x

if __name__ == '__main__':
    with Pool() as p:
        p.apply(f, [1])

CodePudding user response:

Python for MacOS changed the default "start method" in Python 3.8. This means you will now need to protect your code from execution on import by placing all multiprocessing code (and anything else that should only run in the parent process) inside a if __name__ == "__main__": block. You will also run into issues with interactive interpreters like jupyter, or IPython because there isn't always a "main" file to import. It is sometimes possible to configure your IDE to get around this somehow, but the most compatible solution is to run your code from a system terminal. Alternatively you could manually switch back to using "fork" which is faster and uses less memory in some instances, but can fall victim to deadlock when using certain threaded modules (like logging).

  • Related