How can I do `Pipe` or` TTY` with a custom Julia callback?

I would like to present their attachment Julia in MATLAB mex-function, connecting Julia STDIN, STDOUTand STDERRto MATLAB terminal. The documentation for redirect_std[in|out|err]says that the stream I pass as an argument must be TTYeither Pipe(or a TcpSocket, which does not seem to be applicable).

I know how I will determine the correct callbacks for each thread (basically, wrappers around calls in MATLAB inputand fprintf), but I'm not sure how to build the required thread.

+4
source share
1 answer

The pipe was renamed PipeEndpoint at https://github.com/JuliaLang/julia/pull/12739 , but the related documentation was not updated, and PipeEndpoint is now considered internal. However, creating the front of the pipe is still feasible:

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

In addition, a version of these functions with no arguments already creates a pipe object, so the recommended way to do this is:

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

However, all this is less clear than it could be, so I created a migration request to clear this API: https://github.com/JuliaLang/julia/pull/18253 . When this transfer request is combined, the call link_pipewill become unnecessary, or pipeyou can transfer it directly to redirect_stdout. In addition, the return value from the version with no arguments will become regular pipe.

+1
source

Source: https://habr.com/ru/post/1607941/


All Articles