捕获 /重定向ProcessPoolExecutor的所有输出
我正在尝试从ProcessPoolExecutor
捕获所有输出。
想象一下,您有一个文件func.py
:
print("imported") # I do not want this print in subprocesses
def f(x):
return x
然后,您可以使用ProcessPoolExecutor
运行该功能,就像
from concurrent.futures import ProcessPoolExecutor
from func import f # ⚠️ the import will print! ⚠️
if __name__ == "__main__":
with ProcessPoolExecutor() as ex: # ⚠️ the import will happen here again and print! ⚠️
futs = [ex.submit(f, i) for i in range(15)]
for fut in futs:
fut.result()
现在我可以使用EG,contextLib捕获第一个导入的输出。但是,Redirect_stdout
,但是,我也想捕获子过程中的所有输出,然后将它们重定向到主过程的stdout。
在我的真实用例中,我会收到要捕获的警告,但是简单的印刷品会重现问题。
这与防止以下错误相关。
I am trying to capture all output from a ProcessPoolExecutor
.
Imagine you have a file func.py
:
print("imported") # I do not want this print in subprocesses
def f(x):
return x
then you run that function with a ProcessPoolExecutor
like
from concurrent.futures import ProcessPoolExecutor
from func import f # ⚠️ the import will print! ⚠️
if __name__ == "__main__":
with ProcessPoolExecutor() as ex: # ⚠️ the import will happen here again and print! ⚠️
futs = [ex.submit(f, i) for i in range(15)]
for fut in futs:
fut.result()
Now I can capture the output of the first import using e.g., contextlib.redirect_stdout
, however, I want to capture all output from the subprocesses too and redirect them to the stdout of the main process.
In my real use case, I get warnings that I want to capture, but a simple print reproduces the problem.
This is relevant to prevent the following bug https://github.com/Textualize/rich/issues/2371.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论