在Julia 1.0+中:如何使用redirect_stdout获取字符串

Jul*_*ner 3 julia

文档redirect_stdout在1.1.0版本中,我目前使用的,似乎没有给出如何使用该功能的例子。也许我错过了吗?

我想捕获输出println并将其作为字符串返回。

这是一个例子:

julia> VERSION
v"1.1.0"

julia> (rd, wr) = redirect_stdout();

julia> println("This is a test.")

julia> # Get back the string "This is a test."

julia> # s = do_something_with_rd(rd)

julia> # s == "This is a test."

julia> # true
Run Code Online (Sandbox Code Playgroud)

有什么建议么?

编辑

根据以下公认的答案,这是我的问题的完整解决方案:

julia> original_stdout = stdout;

julia> (rd, wr) = redirect_stdout();

julia> println("This is a test.")

julia> s = readline(rd)
"This is a test."

julia> s == "This is a test."
true

julia> redirect_stdout(original_stdout);

julia> println("Test of orig. stdout.")
Test of orig. stdout.
Run Code Online (Sandbox Code Playgroud)

编辑2:更完整的示例

这是使用重定向测试各种printprintln函数输出的示例stdout。感谢@Bogumi吗?卡米斯基(Kami?ski)的回答和编辑使我更加清楚:

using Test

# Test redirect_stdout.
@testset "Example tests using redirect_stdout" begin
    original_stdout = stdout;
    (read_pipe, write_pipe) = redirect_stdout();
    print("Using print function.")
    println("Using println function.")
    println("Second use of println function.")
    println("Line 1.\nLine 2.\nLine 3.\nEND")
    println("""
    This is new line 1.
    This is new line 2. Next a Char = """)
    print('A')
    redirect_stdout(original_stdout);
    close(write_pipe)
    @test readline(read_pipe) == "Using print function.Using println function."
    @test readline(read_pipe) == "Second use of println function."
    @test read(read_pipe, String) == "Line 1.\nLine 2.\nLine 3.\nEND\n" * 
    "This is new line 1.\nThis is new line 2. Next a Char = \nA"
end

# Suppress unnecessary output when this file.
return nothing
Run Code Online (Sandbox Code Playgroud)

这是输出:

julia> include("test_redirect_stdout.jl")
Test Summary:                       | Pass  Total
Example tests using redirect_stdout |    3      3
Run Code Online (Sandbox Code Playgroud)

Bog*_*ski 6

只要运行readlinerd(或任何其它读取操作)。

您只需要注意读操作rd被阻塞,即当操作无法完成时,终端似乎挂起。一种解决方案是为此使用@async。例如:

julia> (rd, wr) = redirect_stdout();

julia> @async global x = readline(rd) # if we did not put @async here the terminal would be blocked
Task (runnable) @0x0000000004e46e10

julia> x # x is yet undefined as readline is waiting for an input
ERROR: UndefVarError: x not defined

julia> println("something") # we feed data to stdout

julia> x # and readline has finished its work and bound the value to variable x
"something"
Run Code Online (Sandbox Code Playgroud)

当然,如果您确切地知道要读取的数据在其中即可运行readline或执行其他功能,而所有这些都可以在没有的情况下工作@async

编辑

鉴于SalchiPapa的评论,我认为这也是添加这种可能的用法的方式,因为最简单的方法是考虑IMO:

original_stdout = stdout
(rd, wr) = redirect_stdout();

println("This is a test 1.")
println("This is a test 2.")
println("This is a test 3.")

redirect_stdout(original_stdout)

# you can still write to wr
println(wr, "This is a test 4.")

# you have to close it to make the read non-blocking
close(wr)

# the pipe is redirected to original stdout and wr is closed so this is non-blocking    
s = read(rd, String)
Run Code Online (Sandbox Code Playgroud)