python - Pool, queue, hang -


i want use queue hold result because want consumer (serial not parallel) process result of workers workers produce result.

for now, want know why following program hangs.

import multiprocessing mp import time import numpy np def worker(arg):     time.sleep(0.2)     q, arr = arg      q.put(arr[0])  p = mp.pool(4) x = np.array([4,4]) q = mp.queue()  in range(4):     x[0] =      #worker((q,x))     p.apply_async(worker, args=((q, x),))   print("done_apply") time.sleep(0.2) in range(4):     print(q.get()) 

queue objects cannot shared. came same conclusion op first finding answer.

unfortunately, there other problems in code (which doesn't make exact duplicate of linked answer)

  • worker(arg) should worker(*arg) args unpacking work. without that, process locked (i admit don't know why. should have thrown exception, guess multiprocessing & exceptions don't work together)
  • passing same x workers result in same number result (with apply works, not apply_async

another thing: code portable, wrap main code if __name__ == "__main__":, required on windows because of differences in process spawning

fully fixed code outputs 0,3,2,1 me:

import multiprocessing mp import time import numpy np def worker(*arg):  # there 2 arguments "worker" #def worker(q, arr):  # better     time.sleep(0.2)     q, arr = arg     q.put(arr[0])  if __name__ == "__main__":     p = mp.pool(4)      m = mp.manager()  # use manager, queue objects cannot shared     q = m.queue()      in range(4):         x = np.array([4,4])  # create array each time (or make copy)         x[0] =         p.apply_async(worker, args=(q, x))      print("done_apply")     time.sleep(0.2)     in range(4):         print(q.get()) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -