joh*_*tis 5 python subprocess process-pool
我正在尝试捕获ProcessPoolExecutor.
想象你有一个文件func.py:
print("imported") # I do not want this print in subprocesses\n\ndef f(x):\n return x\nRun Code Online (Sandbox Code Playgroud)\n然后你用ProcessPoolExecutor类似的方式运行该函数
\nfrom concurrent.futures import ProcessPoolExecutor\nfrom func import f # \xe2\x9a\xa0\xef\xb8\x8f the import will print! \xe2\x9a\xa0\xef\xb8\x8f\n\nif __name__ == "__main__":\n with ProcessPoolExecutor() as ex: # \xe2\x9a\xa0\xef\xb8\x8f the import will happen here again and print! \xe2\x9a\xa0\xef\xb8\x8f\n futs = [ex.submit(f, i) for i in range(15)]\n for fut in futs:\n fut.result()\nRun Code Online (Sandbox Code Playgroud)\n现在我可以使用例如捕获第一次导入的输出contextlib.redirect_stdout,但是,我也想捕获子进程的所有输出并将它们重定向到主进程的标准输出。
在我的实际用例中,我收到了想要捕获的警告,但简单的打印就重现了该问题。
\n\n| 归档时间: |
|
| 查看次数: |
642 次 |
| 最近记录: |