# Printing in a background process

**URL:** https://discourse.julialang.org/t/printing-in-a-background-process/81453
**Category:** General Usage
**Tags:** question
**Created:** [May 22, 2022, 8:34am UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453 "2022-05-22T08:34:02Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![FedeClaudi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedeclaudi/32/33490_2.png) [@FedeClaudi](https://discourse.julialang.org/u/FedeClaudi)
#### Post date: [May 22, 2022, 8:34am UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453/1 "2022-05-22T08:34:02Z")

</div>

Hi everyone,

I’m trying to do something that feels like should be easy but I can’t figure it out.

Basically, let’s say I have a function (`doer`) which takes a while to run and I want to have another function `printer` printing stuff to stdout while `doer` is running - without messing with `doer` itself.

The motivation for this is that in [Term.jl](https://github.com/FedeClaudi/Term.jl) I need to have a function rendering progress bars information while user’s code runs. I can’t edit the user’s code and I want the progress bar rendering to happen continuously in the background.

This is a MWE of what I have so far:

```julia

mutable struct Status
    on::Bool
end

function printer(s::Status)
    println("Printer turning on")
    while s.on
        println("PRINTING")
        sleep(0.01)
    end
    println("Printer turning off")
end

function doer()
    # println("Doer getting started")
    s = Status(true)

    # @async printer(s)
    Base.Threads.@spawn printer(s)
    
    for k in 1:50000
        x = rand(100, 100)
        y = x.^2 .- rand(100, 100)
    end
    s.on = false
    
    nothing
end

print("\n\n")
doer()

```

My expectation is that `printer` should be running in the background while `doer` finishes its stuff, but this is what the output lokos like:

```julia
Printer turning on
Printer turning off

```

I’ve tried using `@async` and `Task` and `@spawn`… same result. The only thing that makes it work is adding a `sleep` call in the loop in `doer`.

Btw: `Threads.nthreads() # 8`.

Any idea on what I’m doing wrong?

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [May 22, 2022, 10:08am UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453/2 "2022-05-22T10:08:46Z")

</div>

Try

```julia
    for k in 1:50000
        x = rand(100, 100)
        y = x.^2 .- rand(100, 100)
        yield() # <-- Allows switching over to other tasks.
    end

```

Or better yet, thread the loop as well

```julia
Threads.@spawn begin 
    for k in 1:50000
        x = rand(100, 100)
        y = x.^2 .- rand(100, 100)
    end
    s.on = false
end

```

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [May 22, 2022, 10:13am UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453/3 "2022-05-22T10:13:29Z")

</div>

I first tried accumulating from the loop so it couldn’t be removed by dead code elimination. That didn’t seem to make a difference. Then I remembered that GC pauses all threads, and I noticed the code was very allocating; I made the operations in the loop non-allocating, and suddenly got tons of printing.

```julia
using Random
function doer()
    # println("Doer getting started")
    s = Status(true)

    # @async printer(s)
    Base.Threads.@spawn printer(s)
    accum = 0.0
    x = zeros(100, 100)
    y = similar(x)
    for k in 1:50000
        rand!(x)
        @. y = x^2 - rand()
        accum += sum(y)
    end
    s.on = false

    return accum
end

```

edit: oops! This was just because I forgot to import `Random`; I lost the error in the sea of printing which never gets turned off… when I fix it, I don’t see the printing.

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [May 22, 2022, 10:15am UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453/4 "2022-05-22T10:15:21Z")

</div>

> [@FedeClaudi](#):
>
> The motivation for this is that in [Term.jl](https://github.com/FedeClaudi/Term.jl) I need to have a function rendering progress bars information while user’s code runs. I can’t edit the user’s code and I want the progress bar rendering to happen continuously in the background.

This might need [Add threadpool support to runtime by jpsamaroo · Pull Request #42302 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/42302#issuecomment-1072785204) which is in Julia 1.9.

---

<div class="post-metadata">

### Author: ![chengchingwen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chengchingwen/32/8390_2.png) [@chengchingwen](https://discourse.julialang.org/u/chengchingwen)
#### Post date: [May 22, 2022, 10:18am UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453/5 "2022-05-22T10:18:41Z")

</div>

`@spawn doer()` also seems to work, I guess thread 1 have higher priority?

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [May 22, 2022, 10:30am UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453/6 "2022-05-22T10:30:58Z")

</div>

Which Julia version are you on? Your GC-free loop is just as blocking for me on Julia 1.7.2 (Windows).

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [May 22, 2022, 10:32am UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453/7 "2022-05-22T10:32:22Z")

</div>

I just realized I had a simple mistake; I edited the post. It is not working for me either now, 1.7.2 MacOS.

---

<div class="post-metadata">

### Author: ![FedeClaudi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedeclaudi/32/33490_2.png) [@FedeClaudi](https://discourse.julialang.org/u/FedeClaudi)
#### Post date: [May 22, 2022, 10:48am UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453/8 "2022-05-22T10:48:27Z")

</div>

Thanks everyon for looking into this, I really appreciate the help.

@skleinbo 'and @chengchingwen had it right: wrapping `doer` or its content in another `@spaw` makes it work like a charm, I’ve also added a `wait` on the task generated by `@spawn doer()` and it does exactly what I need. Progress bar in Term.jl won’t be hanging any more!

@ericphanson thanks for the help, unfortunately I can’t ensure that users’ code (which would be in `doer`) is not allocating!

---

<div class="post-metadata">

### Author: ![chengchingwen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chengchingwen/32/8390_2.png) [@chengchingwen](https://discourse.julialang.org/u/chengchingwen)
#### Post date: [May 22, 2022, 10:53am UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453/9 "2022-05-22T10:53:15Z")

</div>

Maybe it’s worth reading how [ProgressMeter](https://github.com/timholy/ProgressMeter.jl) do or even wrap that? It also support multi-threading and is thread-safe.

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [May 22, 2022, 11:20am UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453/10 "2022-05-22T11:20:04Z")

</div>

A similar topic was already discussed here:

> [@No real multithreading possible?](https://discourse.julialang.org/t/no-real-multithreading-possible/77920):
>
> Hey there, I am desperately trying to figure out how to do some “real” multi-threadding in Julia. What do I mean by that: My program consists of three tasks: the main task and two separate task I start. I need to communicate from the main task to the two separte tasks via channels. This is how I start my desired background task: c = Channel{Bool}(1, spawn=true) do c # create new channel with buffersize 1 status = false while isopen(c) if isready(c) # determine whether the chann…

---

<div class="post-metadata">

### Author: ![FedeClaudi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fedeclaudi/32/33490_2.png) [@FedeClaudi](https://discourse.julialang.org/u/FedeClaudi)
#### Post date: [May 22, 2022, 12:51pm UTC](https://discourse.julialang.org/t/printing-in-a-background-process/81453/11 "2022-05-22T12:51:07Z")

</div>

Thanks, I looked for related topics but had missed this one.
