IOCapture.jl
IOCapture.jl copied to clipboard
Capturing output in multithreaded context
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.
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.