現在我有以下情況:如何讓多處理.Pool()中的某些進程從Python打印到Python中的stdout?
def take_time(time):
sleep(time)
print("Took %d seconds!" %time)
def multip(num_cores, data):
p = multiprocessing.Pool(num_cores)
p.map(take_time, data)
end_time = time()
print("total time taken: %d" %(end_time - start_time))
假設num_cores = 2
和data = (1,1,3)
。
>>> multi(4, data)
Took 1 seconds!
Took 1 seconds!
Took 3 seconds!
這怎麼能渲染,以便一次只有一個進程轉到stdout? 只要沒有兩個印刷品同時印刷,哪個印刷過程並不特別重要。假設我們首先打印對應於的進程。然後,所需的輸出將
>>> multi(4, data)
Took 1 seconds!
Took 3 seconds!
我猜有剛有一個更細齒的工具,而map()
。謝謝!
如果您有三個輸入,您爲什麼只希望得到兩個輸出? –
而不是從'take_time'內打印,你可以返回一個值給你的主線程/進程,並在那裏打印它。 – 101