# Two question on Channel operation

**URL:** <https://discourse.julialang.org/t/two-question-on-channel-operation/40626>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 2, 2020, 6:26pm UTC](https://discourse.julialang.org/t/two-question-on-channel-operation/40626 "2020-06-02T18:26:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [June 2, 2020, 6:26pm UTC](https://discourse.julialang.org/t/two-question-on-channel-operation/40626/1 "2020-06-02T18:26:44Z")

</div>

Qestion1:  
Because the max size of `chnl` is 0, the following code should be blocked. Why the following code is not blocked?

```julia
chnl = Channel() do ch
    foreach(i -> put!(ch, i), 1:4) # not blocked
end;

```

The following is blocked.

```julia
chnl = Channel() 
foreach(i -> put!(ch, i), 1:4) # blocked here

```

Question 2:  
The following result is not as expected.

```julia
function take_channel(c)
    d = take!(c)   
    println("get data: ", d)
    p()
end

function p()
    println("I am here")
end

c = Channel{Int64}(0)
@async take_channel(c) # 
put!(c, 1)

@async take_channel(c)
put!(c, 2)

```

The output is

```julia
get data: 1 #why `get data 2` is not printed, and no `I am here`
    Testing LDTSimulate tests passed

```

If I put the code in `@testset` the result looks as expected,

```julia
function take_channel(c)
    d = take!(c)   
    println("get data: ", d)
    p()
end

function p()
    println("I am here")
end

@testset "test channel" begin
    c = Channel{Int64}(0)
    @async take_channel(c) # 
    put!(c, 1)

    @async take_channel(c)
    put!(c, 2)
end

```

The results are as expected.

```julia
get data: 1
Test Summary: |I am here

get data: 2
I am here
test channel | No tests
    Testing LDTSimulate tests passed 

```

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [June 2, 2020, 7:21pm UTC](https://discourse.julialang.org/t/two-question-on-channel-operation/40626/2 "2020-06-02T19:21:38Z")

</div>

```julia
chnl = Channel() do ch
    foreach(i -> put!(ch, i), 1:4) 
end

```

The inner function **is** blocked, but it’s scheduled asynchronously, so it doesn’t block the main thread. Basically that is similar to doing:

```julia
chnl = Channel() 
@async foreach(i -> put!(ch, i), 1:4)

```

Issue 2 is has to do with scheduling and REPL. I believe if you do:

```julia
@sync begin
    c = Channel{Int64}(0)
    @async take_channel(c) # 
    put!(c, 1)
end

```

The `@sync` will cause the execution to pause and allow the @async task to finish. My guess is that the println() is giving up control which allows the REPL to execute again. You might also be able to do a `sleep(1)` after you do the `put!(c, 1)` which should give the async task time to finish.

---

<div class="post-metadata">

**Author:** ![samuel\_okon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_okon/32/10285_2.png) [@samuel\_okon](https://discourse.julialang.org/u/samuel_okon)\
**Post date:** [June 2, 2020, 7:43pm UTC](https://discourse.julialang.org/t/two-question-on-channel-operation/40626/3 "2020-06-02T19:43:45Z")

</div>

note the `Channel()` constructor which is similar to `Channel(0)` creates an unbuffered channel i.e `put!` blocks until a matching `take!` is called. And vice-versa.  
For me the question 2 works just fine.

```julia
function take_channel(c)
    d = take!(c)   
    println("get data: ", d)
    p()
end

function p()
    println("I am here")
end

c = Channel{Int64}(0)
@async take_channel(c) # 
put!(c, 1);

@async take_channel(c)
put!(c, 2);

```

And i get the expected output.

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [June 3, 2020, 2:30am UTC](https://discourse.julialang.org/t/two-question-on-channel-operation/40626/4 "2020-06-03T02:30:50Z")

</div>

Thank you so much for your answer. The code you suggested works as expected. I am surprised that the default REPL does not run as `@sync`. But it also does not run as @async. There should be some different behaviour not described in the document.

```julia
@sync begin
    c = Channel{Int64}(0)
    @async take_channel(c) # 
    put!(c, 1)
end

```

---

<div class="post-metadata">

**Author:** ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)\
**Post date:** [June 3, 2020, 4:22am UTC](https://discourse.julialang.org/t/two-question-on-channel-operation/40626/5 "2020-06-03T04:22:41Z")

</div>

I found a very clear explanation, much clearer than the document. Hope it could help other users.  
[When to use @sync and @async](https://stackoverflow.com/questions/37287020/how-and-when-to-use-async-and-sync-in-julia)
