How can l launch each worker in a multiprocessing.Pool in a new shell?

て烟熏妆下的殇ゞ 提交于 2019-12-22 00:38:45

问题


I'm trying to couple the execution of a spawned process in a worker pool to a new system terminal. In the following example (adapted from @sylvain-leroux's answer to this question) a pool of workers is constructed to do some work with queued objects.

import os
import time
import multiprocessing

# A main function, to be run by our workers.
def worker_main(queue):
    print('The worker at', os.getpid(), 'is initialized.')
    while True:

        # Block until something is in the queue.
        item = queue.get(True)
        print(item)
        time.sleep(0.5)

if __name__ == '__main__':

    # Instantiate a Queue for communication.
    the_queue = multiprocessing.Queue()

    # Build a Pool of workers, each running worker_main.
    the_pool = multiprocessing.Pool(3, worker_main, (the_queue,))

    # Iterate, sending data via the Queue.
    for i in range(5):
        the_queue.put("That's a nice string you got there.")
        the_queue.put("It'd be a shame if something were to... garble it.")

    worker_pool.close()
    worker_pool.join()
    time.sleep(10)

If you run this from a system terminal you'll see a bunch of garbled text, because each of the workers is writing out to, and executing in, the same console. For an project I'm working on, it would be helpful to spawn a new shell/console to host each worker process, such that all printed output is displayed in that shell, and the execution of the worker process is host in that shell. I've seen several examples doing this with Popen using the shell keyword, but I need to stick to a pool-based implementation, due to compatibility constraints. Has anyone out there done this? Guidance is appreciated.


回答1:


Try using the Queue the other way around.

Let the workers put messages into the Queue, and in the parent process get them from the Queue and print them. That should get rid of intermingled output.

If you want to pass messages both from parent to workers and back, use two queues. One for passing messages to workers, and one to pass messages back to the parent.



来源:https://stackoverflow.com/questions/41413055/how-can-l-launch-each-worker-in-a-multiprocessing-pool-in-a-new-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!