IOCapture.jl icon indicating copy to clipboard operation
IOCapture.jl copied to clipboard

Capturing output in multithreaded context

Open tlienart opened this issue 2 years ago • 1 comments

Hello,

I currently use IOCapture as follows and it works well:

try
    captured = IOCapture.capture() do
        include_string(...)
    end
    # ... processing of captured ...
catch e
    # ...
end

I'd like to be able to do this in a multi-threaded context where these calls potentially happen on separate threads. I can add a ReentrantLock and do

lock(l)
try
   captured[k] = IOCapture.capture() do ... end
catch
   ...
finally
    unlock(l)
end

but this amounts to doing these evaluations sequentially as far as I understand. Is there a better way to capture output of separate threads with IOCapture?

Thanks


Note: I also asked this question in the Suppressor repo (https://github.com/JuliaIO/Suppressor.jl/issues/45) not sure which one of the two would be more amenable to doing this.

tlienart avatar May 10 '22 09:05 tlienart

Under the hood, both IOCapture and Suppressor work basically the same way, so I think they're equally amenable. The basic workhorses are the redirect_stdout and redirect_stderr functions, but I am not sure how well they handle being called from separate threads in parallel. But it would be nice if IOCapture would just work in a multihreaded context.

mortenpi avatar May 11 '22 01:05 mortenpi