1

I am running multiprocessing to shorten my optimization time for multi periods.

Before I was using for loop, which is fast when my data is not large.

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)

The optimize function outputs the optimized result to a csv file.

However, when I changed it to

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.')

everything just stops at the "Pool Closed" output and the optimizer never ends.

Is there any problem with this code?

1 Answer 1

2

You are closing the pool before waiting, you better do it reversed:

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.')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Daniel. I tried the method and an error says: pool.join() File "D:\Anaconda\lib\multiprocessing\pool.py", line 509, in join assert self._state in (CLOSE, TERMINATE) AssertionError

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.