现在我有以下情况:
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?
只要没有两个同时打印,打印哪个过程并不特别重要。假设我们首先打印与data[1]
对应的流程。然后,所需的输出将是
>>> multi(4, data)
Took 1 seconds!
Took 3 seconds!
我猜测只有一个比map()
更精细的齿状工具。谢谢!
答案 0 :(得分:0)
一种策略是只从一个正在运行的线程写入,并将其他线程输出到队列。这样做需要主线程异步启动其他线程(例如,使用map_async),然后从队列中读取它们直到它们完成。举个例子,你会做这样的事情:
q = multiprocessing.Queue()
def take_time(time):
sleep(time)
q.put("Took %d seconds!" %time)
def multip(num_cores, data):
p = multiprocessing.Pool(num_cores)
start_time = time()
result = p.map_async(take_time, data)
while not result.ready():
print q.get()
end_time = time()
print("total time taken: %d" %(end_time - start_time))
有关如何应用此实例的实际示例,check out the code用于docker-compose log
命令,该命令读取可能多个容器化进程的输出并将它们组合成单个流。