【问题标题】:How to redirect the stdout of a multiprocessing.Process如何重定向 multiprocessing.Process 的标准输出
【发布时间】:2019-07-24 02:26:58
【问题描述】:

我使用的是 Python 3.7.4,我创建了两个函数,第一个函数使用 multiprocessing.Process 执行可调用函数,第二个函数只打印“Hello World”。在我尝试重定向标准输出之前,一切似乎都运行良好,这样做会阻止我在流程执行期间获得任何打印值。我已将示例简化到最大程度,这是我目前遇到的问题代码。

这些是我的功能:

import io
import multiprocessing
from contextlib import redirect_stdout


def call_function(func: callable):
    queue = multiprocessing.Queue()                                      
    process = multiprocessing.Process(target=lambda:queue.put(func()))
    process.start()          

    while True:
        if not queue.empty():
            return queue.get()


def print_hello_world():
    print("Hello World")

这行得通:

call_function(print_hello_world)

前面的代码运行成功,打印出“Hello World”

这不起作用:

with redirect_stdout(io.StringIO()) as out:
    call_function(print_hello_world)
print(out.getvalue())

使用前面的代码,我没有在控制台中打印任何内容。

任何建议将不胜感激。我已经能够将问题缩小到这一点,我认为这与 io.StringIO() 已经关闭后结束的过程有关,但我不知道如何检验我的假设,更不知道如何实施解决方案。

【问题讨论】:

    标签: python-3.x python-multiprocessing stringio redirectstandardoutput


    【解决方案1】:

    这是我找到的解决方法。看来,如果我使用文件而不是 StringIO 对象,我可以让事情正常工作。

    with open("./tmp_stdout.txt", "w") as tmp_stdout_file:
        with redirect_stdout(tmp_stdout_file):
            call_function(print_hello_world)
        stdout_str = ""
        for line in tmp_stdout_file.readlines():
            stdout_str += line
        stdout_str = stdout_str.strip()
    
    print(stdout_str)  # This variable will have the captured stdout of the process
    

    另一件可能很重要的事情是多处理库缓冲标准输出,这意味着打印仅在函数执行/失败后显示,为了解决这个问题,您可以在函数内需要时强制标准输出刷新在这种情况下,正在调用的将在 print_hello_world 内部(实际上,我必须为守护进程执行此操作,如果它运行超过指定时间则需要终止)

    sys.stdout.flush()  # This will force the stdout to be printed 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多