【发布时间】:2021-12-07 20:21:43
【问题描述】:
我正在尝试使用以下代码在 python 中使用并行处理:
import os
import datetime
import numpy as np
import FarimaModule
from statsmodels.tsa.arima.model import ARIMA
import matplotlib.pyplot as plt
import multiprocessing as mp
# Here I define some variables: p_max,q_max,m_list,wlen,mstep,fs, listFile
def implement(fname,p_max,q_max,m_list,wlen,mstep,fs):
# It is a really long code
# run the function 'implement' in parallel for different values of the input variable 'fname'
pool = mp.Pool(10)
results = [pool.apply(implement, args=(fname,p_max,q_max,m_list,wlen,mstep,fs)) for fname in listFile]
pool.close()
但它会抛出以下错误:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
其他人发布了同样错误的问题。但我无法实施那里发布的解决方案,因为不清楚如何为我的代码调整这些解决方案。
【问题讨论】:
-
您是否按照错误消息的指示添加了
if __name__ == '__main__':? multiprocessing documentation 解释了该行的必要性。 -
更具体地说,最后三行只需要在主线程中执行。按照您的方式,每个新启动的线程都会读取您的文件并启动另一个由十个线程组成的池。池创建代码只需要执行一次。
-
感谢您的建议。我找不到那里说明的
mp.Pool.apply()方法。但pool.map()似乎工作正常。 -
@FrankYellin 我在此之前添加了
if __name__ == '__main__':' afterpool = mp.Pool. That is why it was not working. It works if I addif name == 'main':'线。但现在看来,它并没有像通常的 for 循环那样按顺序运行;它没有并行化。
标签: python windows multiprocessing