# Multi process parallel computing with RemoteChannel

**URL:** https://discourse.julialang.org/t/multi-process-parallel-computing-with-remotechannel/31149
**Category:** New to Julia
**Tags:** parallel
**Created:** [November 16, 2019, 6:39am UTC](https://discourse.julialang.org/t/multi-process-parallel-computing-with-remotechannel/31149 "2019-11-16T06:39:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AtsushiSakai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atsushisakai/32/6559_2.png) [@AtsushiSakai](https://discourse.julialang.org/u/AtsushiSakai)
#### Post date: [November 16, 2019, 6:39am UTC](https://discourse.julialang.org/t/multi-process-parallel-computing-with-remotechannel/31149/1 "2019-11-16T06:39:30Z")

</div>

Hi. I want do a very simple multi process parallel computing with Remote Channel below:

```julia
using Distributed
 
while nworkers() < 1
    addprocs(1)
end
 
function task(channel, input)
     sleep(5.0)
     push!(channel, input*2)
end
 
function main()
     println("start")
 
     channel = RemoteChannel(()->Channel{Int}(5))
     @spawn task(channel, 1)
 
     while !isready(channel)
         println("result waiting...")
         sleep(1)
     end
 
     println("result:$(take!(channel))")
 
     println("done")
end
 
main()

```

I expected that I can get a result from the task function after 5 secondes.

However, “result waiting…” is shown after 5 seconds…

Can anyone let me know what is wrong in this code?

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [November 16, 2019, 6:55am UTC](https://discourse.julialang.org/t/multi-process-parallel-computing-with-remotechannel/31149/2 "2019-11-16T06:55:53Z")

</div>

i didn’t use any channels, the @spawn macro automatically creates the adequate channel

```julia
using Distributed
 
while nworkers() < 1
    addprocs(1)
end
 
function fun(input)
     sleep(5.0)
     return 2*input
end
 
function main()
     println("start")
 

     my_task = @spawn fun(1)
     res = 0
     while !isready(my_task)
         println("result waiting...")
         sleep(1)
     end
     res = fetch(my_task)
     println("result:$(res)") 
     println("done")
end

```

i dont understand a lot about the use of `RemoteChannel`, though

---

<div class="post-metadata">

### Author: ![AtsushiSakai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atsushisakai/32/6559_2.png) [@AtsushiSakai](https://discourse.julialang.org/u/AtsushiSakai)
#### Post date: [November 16, 2019, 10:33am UTC](https://discourse.julialang.org/t/multi-process-parallel-computing-with-remotechannel/31149/3 "2019-11-16T10:33:30Z")

</div>

Thank you for your comment!!. Using future is a good solution.  
Actually I can do same asynchronous computing with coroutine using this code.

```julia
function fun(channel, input)
     sleep(5.0)
     put!(channel, 2*input)
end
 
function main()
     println("start")
     channel = Channel(1)
     @async fun(channel, 1)

     while !isready(channel)
         println("result waiting...")
         sleep(1)
     end
     res = take!(channel)
     println("result:$(res)") 
     println("done")
end

main()

```

I am looking forward to find a way to do similar computing with multi processing…

---

<div class="post-metadata">

### Author: ![AtsushiSakai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atsushisakai/32/6559_2.png) [@AtsushiSakai](https://discourse.julialang.org/u/AtsushiSakai)
#### Post date: [November 16, 2019, 10:37am UTC](https://discourse.julialang.org/t/multi-process-parallel-computing-with-remotechannel/31149/4 "2019-11-16T10:37:25Z")

</div>

Oh I found my mistake… I have to use put!, not push! in the fun function.  
This code works.

```julia
using Distributed

while nworkers() < 1
    addprocs(1)
end

function task(channel, input)
    sleep(5.0)
    put!(channel, input*2)
end

function main()
    println("start")

    channel = RemoteChannel()
    @spawn task(channel, 1)

    while !isready(channel)
        println("result waiting...")
        sleep(1)
    end

    println("result:$(take!(channel))")

    println("done")
end

time main()

```
