【问题标题】:Separate printing for parallel processesSeparate printing for parallel processes
【发布时间】:2022-12-27 20:11:14
【问题描述】:

I am using pathos.multiprocessing to run a function in parallel processes and with different input arguments per process. Here is a minimum working example:

import pathos.multiprocessing as mp
from time import sleep

def my_func(x, y):
    for i in range(x):
        print(y+i)
        sleep(.2)
    return i + y

seq = [(100, 4), (100, 5)]
processes = 2
print ("Multiprocessing...")
pool = mp.Pool(processes)
resultsObj = pool.starmap_async(my_func, seq )          
pool.close()
results = resultsObj.get()

As expected, the results are printed mixed up from the 2 processes, like so:

Multiprocessing...
4
5
5
6
7
6
7
8
8
9
10
9
10
11

Is there a way to drive the results to 2 different terminal to watch the progress? Or any other way to have the results printed in a "per process" fashion?

【问题讨论】:

    标签: printing multiprocessing pathos


    【解决方案1】:

    I'm the pathos and multiprocess author. One way to split the output is to compose a nested map. Essentially, if you have a function that pipes the input to a terminal, then use a pathos.pools.ThreadPool to map the printing to two different terminals in thread parallel. As you are only working with two processes, then you could just include the function to split the output to the different terminals inside the mapped function you are using above. If you just want to know what process each output is coming from, you can alternately use multiprocess.process.current_process() or os.getpid().

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2021-06-08
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多