# How to capture stdout from one Thread?

**URL:** <https://discourse.julialang.org/t/how-to-capture-stdout-from-one-thread/45812>\
**Category:** General Usage\
**Created:** [August 31, 2020, 1:13am UTC](https://discourse.julialang.org/t/how-to-capture-stdout-from-one-thread/45812 "2020-08-31T01:13:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danspielman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danspielman/32/4708_2.png) [@danspielman](https://discourse.julialang.org/u/danspielman)\
**Post date:** [August 31, 2020, 1:13am UTC](https://discourse.julialang.org/t/how-to-capture-stdout-from-one-thread/45812/1 "2020-08-31T01:13:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 31, 2020, 4:06am UTC](https://discourse.julialang.org/t/how-to-capture-stdout-from-one-thread/45812/2 "2020-08-31T04:06:30Z")

</div>

> **[Slack is your productivity platform](https://julialang.slack.com/?redir=%2Farchives%2FC6A044SQH%2Fp1598835925011700)**
>
> Slack is a new way to communicate with your team. It’s faster, better organized, and more secure than email.

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

---

<div class="post-metadata">

**Author:** ![danspielman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danspielman/32/4708_2.png) [@danspielman](https://discourse.julialang.org/u/danspielman)\
**Post date:** [August 31, 2020, 1:47pm UTC](https://discourse.julialang.org/t/how-to-capture-stdout-from-one-thread/45812/3 "2020-08-31T13:47:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [August 31, 2020, 2:34pm UTC](https://discourse.julialang.org/t/how-to-capture-stdout-from-one-thread/45812/4 "2020-08-31T14:34:15Z")

</div>

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](https://pbayer.github.io/DiscreteEvents.jl/dev/processes/#Now) in [`DiscreteEvents`](https://github.com/pbayer/DiscreteEvents.jl).

---

<div class="post-metadata">

**Author:** ![captchanjack1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/captchanjack1/32/28877_2.png) [@captchanjack1](https://discourse.julialang.org/u/captchanjack1)\
**Post date:** [September 4, 2021, 6:49am UTC](https://discourse.julialang.org/t/how-to-capture-stdout-from-one-thread/45812/5 "2021-09-04T06:49:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cjproud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjproud/32/38494_2.png) [@cjproud](https://discourse.julialang.org/u/cjproud)\
**Post date:** [October 28, 2022, 4:25am UTC](https://discourse.julialang.org/t/how-to-capture-stdout-from-one-thread/45812/6 "2022-10-28T04:25:27Z")

</div>

Hey guys, any update on this? I’ve got a process which runs separately on multiple threads of which I want to capture the output of each. `redirect_stdout` lumps the output into a single text file.
