I am trying to use parallel processing in python using the following code:
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()
But it throws the following error:
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.
Others have posted questions with the same error. But I am not able to implement solutions posted there as it is unclear how do I adapt those solutions for my code.
if __name__ == '__main__':as the error message indicates? The multiprocessing documentation explains the need for that line.mp.Pool.apply()method illustrated there. But thepool.map()seems to be working.if __name__ == '__main__':' afterpool = mp.Pool. That is why it was not working. It works if I addif name == 'main':' before this line. But now it seems that it is not running in sequence like a usual for loop; it is not parallelizing.