How to capture stdout from one Thread?

I would like to capture the stdout from one thread out of many that are running, and save it in a string.
Does anyone know how to do this thread-by-thread? I can at least know the id of the thread I care about.

redirect_stdout does not seem like the right solution, because it redirects the outputs for all threads.
I can’t necessarily edit the code, because not all of it is mine. And, some could be written in C.

https://julialang.slack.com/archives/C6A044SQH/p1598835925011700

in short: redirect_stdout into IOBuffer or use Suppressor.jl per @ianshmean 's reply on Slack

This doesn’t work because it redirects stdout from ALL threads, whereas I want to separate output by the thread that generated it.

Of course, redirecting stdout from all threads would be ok if it were possible to tell which thread each entry in the stream comes from. But, I haven’t found a way to do that.

This is a similar problem to printing from asynchronous tasks, which can interfere with each other. I would propose to have one handler – a kind of print server, may be on thread one – to which the other ones can print. The handler collects the jobs and puts them out sequentially. You must provide a method to Base.print.

I do something similar in DiscreteEvents.

1 Like

I’m having a similar issue, did you manage to find a solution? Making a custom dispatch print function only solves the problem partially