如何使用multiproceesing.pool和queue打印(某物)

时间:2018-01-13 13:28:00

标签: python queue multiprocessing pool

from multiprocessing import Pool
import multiprocessing
import queue
import time

def myTask(queue):
  value = queue.get()
  print("Process {} Popped {} from the shared Queue".format(multiprocessing.current_process().pid, value))
  queue.task_done()

def main():
  m = multiprocessing.Manager()
  sharedQueue = m.Queue()
  sharedQueue.put(2)
  sharedQueue.put(3)
  sharedQueue.put(4)

  process1 = multiprocessing.Process(target=myTask, args=(sharedQueue,))
  process1.start()
  process1.join()

  process2 = multiprocessing.Process(target=myTask, args=(sharedQueue,))
  process2.start()
  process2.join()

  process3 = multiprocessing.Process(target=myTask, args=(sharedQueue,))
  process3.start()
  process3.join()


if __name__ == '__main__':
  main()
  print("Done")

我想打印下面的句子。

print(“Process {} Popped {}来自共享队列”.format(multiprocessing.current_process()。pid,value))

但这里有一些问题

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我认为Pool不适用于您想要做的事情。它应该用于并行运行具有已定义参数的特定函数,因此必须在请求池运行之前读取队列:

import multiprocessing # I just call & use multiprocessing (queue is included)

def my_function(value):
    """ the function to run """
    print("Process {} Popped {} from the shared Queue".format(multiprocessing.current_process().pid, value), flush=True)

def main():
    # Create the queue
    shared_queue = multiprocessing.Queue()
    # Fill the queue
    shared_queue.put_nowait(2)
    shared_queue.put_nowait(3)
    shared_queue.put_nowait(4)

    # Create the pool
    p = multiprocessing.Pool(3)
    # Pass the content of the queue as list to the pool
    p.map(my_function, [shared_queue.get() for i in range(shared_queue.qsize())])

if __name__ == '__main__':
    main()
    print("Done")