在 Mac 上给出 AttributeError 的多处理示例
Multiprocessing example giving AttributeError on Mac
最近我在Mac上重新安装了最新的Anaconda3,发现
有一些错误
from multiprocessing import Pool
def f(x):
return x*x
with Pool(5) as p:
p.apply(f, [1])
错误如
AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>
但是同样的代码几天前就可以工作了!!!
试试这个:
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool() as p:
p.apply(f, [1])
Python for MacOS 将默认值 "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 改回使用“fork”,它在某些情况下速度更快且使用的内存更少,但在使用某些线程模块时可能会陷入死锁(例如logging
).
最近我在Mac上重新安装了最新的Anaconda3,发现
有一些错误from multiprocessing import Pool
def f(x):
return x*x
with Pool(5) as p:
p.apply(f, [1])
错误如
AttributeError: Can't get attribute 'f' on <module '__main__' (built-in)>
但是同样的代码几天前就可以工作了!!!
试试这个:
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool() as p:
p.apply(f, [1])
Python for MacOS 将默认值 "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 改回使用“fork”,它在某些情况下速度更快且使用的内存更少,但在使用某些线程模块时可能会陷入死锁(例如logging
).