Giter VIP home page Giter VIP logo

Comments (6)

mortenpi avatar mortenpi commented on July 3, 2024 1

This seems like a good feature to have here. I would indeed call it something other than tee though, since that does feel a bit jargony.

from iocapture.jl.

goerz avatar goerz commented on July 3, 2024 1

I'm going to play around a little and see if I come up with something that works

from iocapture.jl.

goerz avatar goerz commented on July 3, 2024

For context: https://discourse.julialang.org/t/a-tee-option-for-iocapture/108642

from iocapture.jl.

goerz avatar goerz commented on July 3, 2024

Well, here's a start:

output = IOBuffer()
temp = IOBuffer()
buffer_redirect_task = @async begin
    write(temp, pipe)
    temp_data = take!(temp)
    write(output, temp_data)
    write(default_stdout, temp_data)
end

This seems to capture the data from the pipe while also passing it through the normal stdout.

Is this a good idea, or does this have the potential to block something or slow things down at a low level? If there's an expert on the IO internals that can sign off on this, I can easily build that into a full PR.

from iocapture.jl.

goerz avatar goerz commented on July 3, 2024

This might work, too, and avoid the allocation of temp_data?

output = IOBuffer()
temp = IOBuffer()
buffer_redirect_task = @async begin
    n_bytes = write(temp, pipe)
    seek(temp, 0)
    write(output, read(temp, n_bytes))
    seek(temp, 0)
    write(default_stdout, read(temp, n_bytes))
end

from iocapture.jl.

goerz avatar goerz commented on July 3, 2024

Ugh… neither of these actually works. I should have tested it on a longer-running calculation to start with. It seems like the writing to default_stdout gets buffered until the task ends. I've tried adding a flush(default_stdout), too.

Update: this one works better:

bufsize = 128
buffer = Vector{UInt8}(undef, bufsize)
buffer_redirect_task = @async begin
    while !eof(pipe)
        nbytes = readbytes!(pipe, buffer, bufsize)
        data = view(buffer, 1:nbytes)
        write(output, data)
        write(default_stdout, data)
    end
end

from iocapture.jl.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.