这是我第一次在python中使用并行程序,我的程序如下所示:
np.random.RandomState(100)
arr = np.random.randint(0, 10, size=[200000, 5])#print(arr)
data = arr.tolist()
data[:5]
def howmany_within_range(row, minimum, maximum):
count = 0
for n in row:
if minimum <= n <= maximum:
count = count + 1
return count
pool = mp.Pool(mp.cpu_count())
results = [pool.apply(howmany_within_range, args=(row, 4, 8)) for row in data]
pool.close()
print(results[:10])
该程序的执行产生以下错误:
回溯(最近通话最近): 文件“”,第1行,位于 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ spawn.py”,行105,在spawn_main中 退出代码= _main(fd) 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ spawn.py”,第114行,位于_main 准备(准备数据) 准备文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ spawn.py”,行225 _fixup_main_from_path(data ['init_main_from_path']) 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ spawn.py”,第277行,位于_fixup_main_from_path中 run_name =“ mp_main ”) 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ runpy.py”,行263,在run_path中 pkg_name = pkg_name,script_name = fname) 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ runpy.py”,第96行,_run_module_code mod_name,mod_spec,pkg_name,script_name) 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ runpy.py”,第85行,_run_code exec(代码,run_globals) 文件“ C:\ 109_personel \ 113_ARIMA_Tmin \ Test_2.py”,第24行,在 池= mp.Pool(mp.cpu_count())跟踪(最近一次通话最近):
文件“”,第1行,在 池中的文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ context.py”,行119 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ spawn.py”,行105,在spawn_main中 context = self.get_context()) init 中的文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ pool.py”,第176行 退出代码= _main(fd) 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ spawn.py”,第114行,位于_main self._repopulate_pool() _repopulate_pool中的文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ pool.py”,行241 准备(准备数据) 准备文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ spawn.py”,行225 w.start() 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ process.py”,开始 _fixup_main_from_path(data ['init_main_from_path']) 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ spawn.py”,第277行,位于_fixup_main_from_path中 self._popen = self._Popen(自身) _Popen中的文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ context.py”,第322行 run_name =“ mp_main ”) 返回Popen(process_obj)文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ runpy.py”,行263,在run_path中
init 中的文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ popen_spawn_win32.py”,第33行 prep_data = spawn.get_preparation_data(process_obj._name) get_preparation_data中的第143行的文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ spawn.py” pkg_name = pkg_name,script_name = fname) 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ runpy.py”,第96行,_run_module_code _check_not_importing_main() 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ multiprocessing \ spawn.py”,行136,在_check_not_importing_main中 追溯(最近一次通话): 文件“”,第1行,位于 mod_name,mod_spec,pkg_name,script_name) 文件“ C:\ Users \ lenovo \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ runpy.py”,第85行,_run_code 不会被冻结以生成可执行文件。''') RuntimeError: 尝试在启动新进程之前启动新进程。 当前进程已完成其引导阶段。
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.
exec(code, run_globals)
任何人都可以帮助我解决此错误。