如何在Julia中使用自定义回调制作`Pipe`或`TTY`?

我想通过将Julia的STDIN,STDOUT和STDERR连接到MATLAB终端来想象我将Julia嵌入到MATLAB mex函数中. redirect_std [in | out | err]的文档说我作为参数传入的流需要是TTY或Pipe(或TcpSocket,似乎不适用).

我知道如何为每个流定义正确的回调(基本上,围绕调用MATLAB的输入和fprintf的包装),但我不确定如何构造所需的流.

最佳答案 Pipe在
https://github.com/JuliaLang/julia/pull/12739中重命名为PipeEndpoint,但相应的文档未更新,PipeEndpoint现在被视为内部.即便如此,创建前面的管道仍然是可行的:

pipe = Pipe()
Base.link_pipe(pipe)
redirect_stdout(pipe.in)
@async while !eof(pipe)
    data = readavailable(pipe)
    # Pass data to whatever function handles display here
end

此外,这些函数的无参数版本已经创建了一个管道对象,因此推荐的方法是:

(rd,wr) = redirect_stdout()
@async while !eof(rd)
    data = readavailable(rd)
    # Pass data to whatever function handles display here
end

然而,所有这些都不太清楚,所以我创建了一个拉取请求来清理这个API:https://github.com/JuliaLang/julia/pull/18253.一旦拉出请求被合并,link_pipe调用将变得不必要,管道可以直接传递到redirect_stdout.此外,无参数版本的返回值将成为常规管道.

点赞