我的代码在多个子进程上并行运行一个函数(取自外部库(。这个库函数包括一个简单的进度条,它可以不时地打印和刷新到stdout。我想知道每次刷新stdout时打印的是哪个子进程。但是,我希望避免更改与库函数相关的任何内容,因为我不想拥有该库的私有版本。这可能吗?
下面是我的代码的一个典型示例:
import multiprocessing as mp
import sys
from library import library_function
def my_function(parameter):
# do some stuff
do_stuff()
# run library function
library_function()
return
if __name__ == '__main__':
with mp.Pool() as pool:
pool.map(my_function, parameters)
# library_function typically looks like
def library_function():
for i in range(n):
# do some things
do_things()
# print the iteration index
print(f'iteration {i+1}/{n}')
sys.stdout.flush()
return
如果我这样运行代码,我将不知道哪个子进程在哪个迭代中。我希望输出看起来像:
[Child 0] iteration 1/3
[Child 1] iteration 1/3
[Child 1] iteration 2/3
[Child 0] iteration 2/3
[Child 0] iteration 3/3
[Child 2] iteration 1/3
[Child 1] iteration 3/3
[Child 2] iteration 2/3
[Child 2] iteration 3/3
我想这会给你一个想法:
from contextlib import redirect_stdout
from io import StringIO
from multiprocessing import current_process
def my_function():
f = StringIO()
with redirect_stdout(f):
library_function()
s = f.getvalue()
lines = s.splitlines()
pid = current_process().pid
for line in lines:
print(f'[{pid}] {line}', flush=True)
def library_function():
for i in range(4):
print('This is line', i, flush=True)
my_function()
打印:
[18492] This is line 0
[18492] This is line 1
[18492] This is line 2
[18492] This is line 3
如果您不想使用进程id,您可以始终向my_function
传递一个额外的参数,process_number,该参数从1开始变化。。N、 其中N是子进程的数量。