# Hello world, and Julia behaves strangely

**URL:** <https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 22, 2022, 10:35pm UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866 "2022-04-22T22:35:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![emo](https://avatars.discourse-cdn.com/v4/letter/e/ecc23a/32.png) [@emo](https://discourse.julialang.org/u/emo)\
**Post date:** [April 22, 2022, 10:35pm UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866/1 "2022-04-22T22:35:30Z")

</div>

Hi,

I’m new to this community, so hello everyone!

I’m learning Julia and I get some strange output on a very simple program. I don’t understand what I’m doing wrong. Could someone help me please?

This is an experiment with a task and a channel., here is the code.

```julia
chan = Channel(1)

waiter = @task begin

    while true

        wait(chan)
        msg = take!(chan)
        println("got ", msg)
        if msg == "stop" break end
    end
end

sleep(2)
schedule(waiter)
println("scheduled")

sleep(1)
println("sending string msg")
put!(chan, "message")

# sleep(1)
# println("sending number msg")
# put!(chan, 123)

```

I get the following result below. Notice the extra double-quote that shouldn’t be there after the word message:

```julia
julia> include("test.jl")
scheduled
sending string msg
got message"
message"

```

And if I uncomment the last 3 lines to add a second message, it gets even weirder, the double quote disappears and the number 123 is repeated twice:

```julia
julia> include("test.jl")
scheduled
sending string msg
got message
sending number msg
got 123123

```

So I guess there must be something I’m doing wrong, I keep reading the docs, but I don’t understand why it behaves like this.  
Julia Version 1.7.2 (2022-02-06) in a terminal of VSCode on Windows.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [April 22, 2022, 10:45pm UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866/2 "2022-04-22T22:45:18Z")

</div>

Since you run it in the repl it will always print the value of the evaluated expression, here it seems like put! might also return the value it inserts and that is maybe what is added to the end.  
The formatting of the last lines seems a bit strange, but on the phone so cant test it.

---

<div class="post-metadata">

**Author:** ![emo](https://avatars.discourse-cdn.com/v4/letter/e/ecc23a/32.png) [@emo](https://discourse.julialang.org/u/emo)\
**Post date:** [April 22, 2022, 10:47pm UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866/3 "2022-04-22T22:47:14Z")

</div>

Wait I got it. There’s a newline at the beginning of the string which is the result of the call. Same for 123, I get 123 from the waiter, and then 123 as a return value of the call.

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [April 23, 2022, 1:42am UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866/4 "2022-04-23T01:42:35Z")

</div>

Sorry, I don’t follow: where did the nl come from?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [April 23, 2022, 4:40am UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866/5 "2022-04-23T04:40:23Z")

</div>

If you have a script and want to suppress the output of the value of the last statement, just add

```julia
nothing

```

as last line.

Same holds for functions, if they shall not return anything use `nothing` as last statement.

---

<div class="post-metadata">

**Author:** ![emo](https://avatars.discourse-cdn.com/v4/letter/e/ecc23a/32.png) [@emo](https://discourse.julialang.org/u/emo)\
**Post date:** [April 23, 2022, 9:38am UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866/6 "2022-04-23T09:38:08Z")

</div>

Thanks for the tip.

I don’t know where the newline comes from. To me, it shouldn’t be there. The newline explains the output, but the newline itself is not explained.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 23, 2022, 10:08am UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866/7 "2022-04-23T10:08:59Z")

</div>

> [@ufechner7](#):
>
> just add
> 
> ```julia
> nothing
> 
> ```
> 
> as last line.

Or

```julia
return nothing 

```

Some think that this is better style.

---

<div class="post-metadata">

**Author:** ![emo](https://avatars.discourse-cdn.com/v4/letter/e/ecc23a/32.png) [@emo](https://discourse.julialang.org/u/emo)\
**Post date:** [April 23, 2022, 10:43am UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866/8 "2022-04-23T10:43:12Z")

</div>

Understood, thank you!

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [April 23, 2022, 1:19pm UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866/9 "2022-04-23T13:19:03Z")

</div>

You have a race condition in your output to stdout.

The newline, you don’t expect is from your waiter tasks println command.

The race condition, which produces strange behavior, is that you use stdout io stream in both threads (main+waiter). Additional to your `println` commands, `show` is called for the return value of `put!`.  
The channels `put!` and `take!` are thread-safe as they call `lock`/`unlock` on the channel, but `println` is not, because the io stream STDOUT, which is typically buffered too, is not synchonized via `lock`/`unlock`.

You can have it even more weird, for example, if you supress the `show` output, by putting a `;` at the end of the `put!` command:

```julia
julia> chan = Channel(1)
Channel{Any}(1) (empty)

julia> waiter = @task begin
           while true
               wait(chan)
               msg = take!(chan)
               println("got ",msg)
               if msg == "stop" break end
           end
       end
Task (runnable) @0x000000000b7d2e90

julia> schedule(waiter)
Task (runnable) @0x000000000b7d2e90

julia> put!(chan, "xxxxx");
got
julia>

```

The output of `println("got ",msg)` is at some time point disrupted by clearing STDOUT, so only `"got "` made it through. But if you change  
`println("got ",msg)`  
to  
`println("got "*msg)`  
the output gets through:

```julia
julia> chan = Channel(1)
Channel{Any}(1) (empty)

julia> waiter = @task begin
           while true
               wait(chan)
               msg = take!(chan)
               println("got "*msg)
               if msg == "stop" break end
           end
       end
Task (runnable) @0x000000000b202e90

julia> schedule(waiter)
Task (runnable) @0x000000000b202e90

julia> put!(chan, "xxxxx");
got xxxxx

julia> 

```

because the complete output string is composed before writing to STDOUT and writing to STDOUT is done once by `println`. But beware, using string concatenation before calling `println` is NOT the general solution here, because it depends on the buffer size of STDOUT. If several threads needs to write to STDOUT you have to synchronize it on your own.

If you change your original code from  
`println("got ", msg)`  
to  
`print("got "*msg*"\n")`  
you probably wouldn’t have started this thread 🙂

```julia
julia> chan = Channel(1)
Channel{Any}(1) (empty)

julia> waiter = @task begin
           while true
               wait(chan)
               msg = take!(chan)
               print("got "*msg*"\n")
               if msg == "stop" break end
           end
       end
Task (runnable) @0x000000000b242e90

julia> schedule(waiter)
Task (runnable) @0x000000000b242e90

julia> put!(chan, "message")
got message
"message"

```

All looks good, but it is only because buffer size is large enough for these short examples.

So, you are messing with stdout in a non thread-safe way and that produces weird outputs.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [April 23, 2022, 1:21pm UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866/10 "2022-04-23T13:21:26Z")

</div>

Right, with additional “sleep” it works:

```julia
chan = Channel(1)
waiter = @task begin
    while true
        wait(chan)
        msg = take!(chan)
        println("got ", msg)
        if msg == "stop" break end
    end
end

sleep(2)
schedule(waiter)
println("scheduled")

sleep(1)
println("sending string msg")
put!(chan, "message")

sleep(4)
println("oo")

nothing

```

gives

```julia
julia> include("junk.jl")                                          
scheduled                                                          
sending string msg                                                 
got message                                                        
oo                                                                 
                                                                   
julia> 

```

As @oheil explained, the printing needs to be separated from the asynchronous execution.

---

<div class="post-metadata">

**Author:** ![emo](https://avatars.discourse-cdn.com/v4/letter/e/ecc23a/32.png) [@emo](https://discourse.julialang.org/u/emo)\
**Post date:** [April 23, 2022, 4:57pm UTC](https://discourse.julialang.org/t/hello-world-and-julia-behaves-strangely/79866/11 "2022-04-23T16:57:45Z")

</div>

I’m surprised, I didn’t expect that. I guess my past with Javascript explains my mistake. In Julia, parallel is… really parallel.

So the general solution is to take care of it, properly handling concurrent access to STDOUT, by setting up a synchronizing mechanism. For example, a channel with a consumer that writes messages to STDOUT, if I understand correctly.

Thanks a lot for the time you took to explain it. You help me think in Julia.  
🙂
