# How does io redirection work with tasks?

**URL:** https://discourse.julialang.org/t/how-does-io-redirection-work-with-tasks/112405
**Category:** General Usage
**Tags:** task, async
**Created:** [April 2, 2024, 8:46am UTC](https://discourse.julialang.org/t/how-does-io-redirection-work-with-tasks/112405 "2024-04-02T08:46:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [April 2, 2024, 8:46am UTC](https://discourse.julialang.org/t/how-does-io-redirection-work-with-tasks/112405/1 "2024-04-02T08:46:30Z")

</div>

I don’t understand this simple MWE:

```julia
julia> redirect_stdio(;stdout = devnull) do
           @async println("hello")
           @async redirect_io(; stdout = devnull) do
               println("hey")
           end
           println("hi")
       end
hello

```

The docstring of `redirect_stdio` doesn’t mention anything about it not affecting child tasks. How does this actually work?

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [April 2, 2024, 9:46pm UTC](https://discourse.julialang.org/t/how-does-io-redirection-work-with-tasks/112405/2 "2024-04-02T21:46:33Z")

</div>

The `do` block seems to complete without interruption, the streams restored, and then the tasks run. If you do something to interrupt the `do` block and go to the scheduler:

```julia
julia> redirect_stdio(;stdout = devnull) do
           @async println("hello")
           sleep(0)
           @async redirect_io(; stdout = devnull) do
               println("hey")
           end
           println("hi")
       end

```

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [April 2, 2024, 11:56pm UTC](https://discourse.julialang.org/t/how-does-io-redirection-work-with-tasks/112405/3 "2024-04-02T23:56:28Z")

</div>

My preference is to never let a block return if tasks created in that block are still running.

```julia
julia> Base.redirect_stdio(;stdout = devnull) do
               @sync begin
                  @async println("hello")
                  @async Base.redirect_stdio(; stdout = devnull) do
                      println("hey")
                  end
                  println("hi")
               end
              end

julia> 

```
