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


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 {} from the shared Queue".format(multiprocessing.current_process((.pid, value((

但是这里有一些问题

我该怎么做?

我认为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")

相关内容

  • 没有找到相关文章

最新更新