gpt4 book ai didi

julia - 在 Julia 1.0+ : How do I get strings using redirect_stdout

转载 作者:行者123 更新时间:2023-12-04 12:14:08 24 4
gpt4 key购买 nike

documentationredirect_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

有什么建议?

编辑

根据下面接受的答案,这里是我的问题的完整解决方案:
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.

编辑 2:更完整的示例

下面是一个测试各种 print的例子和 println使用 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

这是输出:
julia> include("test_redirect_stdout.jl")
Test Summary: | Pass Total
Example tests using redirect_stdout | 3 3

最佳答案

就跑 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"

当然,如果您确切地知道要读取的数据在那里,只需运行 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)

关于julia - 在 Julia 1.0+ : How do I get strings using redirect_stdout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54599148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com