# Printing from a task

**URL:** <https://discourse.julialang.org/t/printing-from-a-task/43698>\
**Category:** General Usage\
**Tags:** question, parallel, multithreading\
**Created:** [July 26, 2020, 7:58am UTC](https://discourse.julialang.org/t/printing-from-a-task/43698 "2020-07-26T07:58:52Z")\
**Posts on this page:** 2\
**Page:** 1

<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:** [July 26, 2020, 7:58am UTC](https://discourse.julialang.org/t/printing-from-a-task/43698/1 "2020-07-26T07:58:52Z")

</div>

In testing my package I have problems to print from an asynchronous task to the REPL. Here is a MWE:

```julia
struct Msg
    a
    b
end

function tpr(ch::Channel)
    while true
        msg = take!(ch)
        if msg isa Msg
            println(msg.a, " ", msg.b) # should print one line + CR
        else
            break
        end
    end
end

ch = Channel(tpr) # I setup tpr as a task on a channel

```

If I send a message to my task, I get the following:

```julia
julia> put!(ch, Msg("first", "second"))
first secondMsg
("first", "second")

julia> put!(ch, Msg("first", "second"));
first 
julia> 
julia> 

```

1. case (without `;`) I get the `CR` after the command has returned (not after the task printout).
2. case (with `;`) I am loosing the 2nd message argument entirely.

Am I missing something? How can I print correctly from a task? Any advice?

> **versioninfo()**
>
> ```julia
> julia> versioninfo()
> Julia Version 1.4.2
> Commit 44fa15b150* (2020-05-23 18:35 UTC)
> Platform Info:
> OS: macOS (x86_64-apple-darwin18.7.0)
> CPU: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
> WORD_SIZE: 64
> LIBM: libopenlibm
> LLVM: libLLVM-8.0.1 (ORCJIT, skylake)
> Environment:
> JULIA_NUM_THREADS = 8
> JULIA_EDITOR = atom -a
> 
> ```

---

<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:** [July 26, 2020, 9:57am UTC](https://discourse.julialang.org/t/printing-from-a-task/43698/2 "2020-07-26T09:57:38Z")

</div>

Now I found one solution: Since obviously my `println` command with multiple arguments causes as many times a `yield()`, I have to compose my output string before `print`ing it. With

```julia
using Printf

function tpr(ch::Channel)
    ...
        print(@sprintf("%s %s\n", msg.a, msg.b))
    ...
end

```

I get the right printouts:

```julia
julia> put!(ch, Msg("first", "second"))
first second
Msg("first", "second")

julia> put!(ch, Msg("first", "second"));
first second

julia> 

```
