# Simple communication between tasks

**URL:** https://discourse.julialang.org/t/simple-communication-between-tasks/86480
**Category:** General Usage
**Created:** [August 29, 2022, 12:33am UTC](https://discourse.julialang.org/t/simple-communication-between-tasks/86480 "2022-08-29T00:33:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [August 29, 2022, 12:33am UTC](https://discourse.julialang.org/t/simple-communication-between-tasks/86480/1 "2022-08-29T00:33:53Z")

</div>

I’d like to have two simple tasks, that would symmetrically send values to each other.

```julia
function task1(other_task)
    res1 = send_to_other_task(other_task, 10) # switch to task2 and blocks until it sends a value
    @show res1
    res2 = send_to_other_task(other_task, 15)
    @show res2
end

function task2(other_task, starting_value)
    @show starting_value
    result1 = send_to_other_task(other_task, 100)
    @show result1
end

julia> launch_communicating_tasks(task1, task2)
starting_value = 10
res1 = 100
result1 = 15
res2 = :end_of_task # or somesuch flag value

```

How should I implement that? I tried `yield`, `yieldto` (both of which don’t seem to handle other-task exceptions natively, so it’s pretty rough going?), having two channels (but channels can switch on IO, which is definitely not what I’m going for, and they don’t really block the way I want?) Having a single one-value channel, and using a closed-over ref to pass values the other way around…

But well, the code is never behaving as I’d expect, and I get a hung REPL all the time… Can anyone give some high-level pointers? I’m sure I can figure out the details, but the docs are pretty sparse.

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [August 29, 2022, 6:04am UTC](https://discourse.julialang.org/t/simple-communication-between-tasks/86480/2 "2022-08-29T06:04:26Z")

</div>

I’ve made progress using `yieldto`, with a `try` block in the subtask that sends the exception + backtrace back to the main task. Looks workable…

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [August 29, 2022, 9:25am UTC](https://discourse.julialang.org/t/simple-communication-between-tasks/86480/3 "2022-08-29T09:25:10Z")

</div>

Channel constructors take buffer size as an argument. Buffered channels should block like you want.

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [August 29, 2022, 1:14pm UTC](https://discourse.julialang.org/t/simple-communication-between-tasks/86480/4 "2022-08-29T13:14:30Z")

</div>

```julia
julia> c = Channel(1) do c
       for i in 1:10
       println("About to put $i")
       put!(c, i)
       end
       end
About to put 1
About to put 2
Channel{Any}(1) (2 items available)

```

Channels run until they can’t put the next argument (I’m guessing, for speed reasons?). `put!` doesn’t switch to the next task unless it can’t put its argument, which defeats the purpose. I believe that I can have a second `put!(c, :dummy_put)` to force blocking, but I couldn’t make it to work. Maybe my logic was wrong…
