在for循环工作时,Python多处理不起作用

时间:2017-06-27 08:40:52

标签: python python-3.x multiprocessing python-multiprocessing

我正在运行多处理以缩短多个时段的优化时间。

在我使用for循环之前,当我的数据不大时,这很快。

for date in DatesOpt:
  x = X.loc[X['Date'] == np.int(date)].drop('Date',1) 
  f = F.loc[F['Date'] == np.int(date)].drop('Date',1)
  d = D.loc[D['Date'] == np.int(date)].drop('Date',1)
  r = R.loc[R['Date'] == np.int(date)].drop('Date',1)
  optimize(date,x,f,d,r)

optimize函数将优化结果输出到csv文件。

但是,当我把它改为

if __name__=='__main__':
  pool = mp.Pool(mp.cpu_count()-1)
  for date in DatesOpt:
    x = X.loc[X['Date'] == np.int(date)].drop('Date',1) 
    f = F.loc[F['Date'] == np.int(date)].drop('Date',1)
    d = D.loc[D['Date'] == np.int(date)].drop('Date',1)
    r = R.loc[R['Date'] == np.int(date)].drop('Date',1)
    pool.apply_async(optimize,args=(date,x,f,d,r,))
  print('Waiting for all subprocesses done')
  pool.close()
  print('Pool Closed')
  pool.join()
  print('All subprocess done.')

一切都在“Pool Closed”输出处停止,优化器永远不会结束。

此代码有问题吗?

1 个答案:

答案 0 :(得分:2)

你在等待之前关闭游泳池,你最好逆转:

if __name__=='__main__':
  pool = mp.Pool(mp.cpu_count()-1)
  for date in DatesOpt:
    x = X.loc[X['Date'] == np.int(date)].drop('Date',1) 
    f = F.loc[F['Date'] == np.int(date)].drop('Date',1)
    d = D.loc[D['Date'] == np.int(date)].drop('Date',1)
    r = R.loc[R['Date'] == np.int(date)].drop('Date',1)
    pool.apply_async(optimize,args=(date,x,f,d,r,))
  print('Waiting for all subprocesses done')
  pool.join()
  pool.close()
  print('Pool Closed')
  print('All subprocess done.')