Hi. I want do a very simple multi process parallel computing with Remote Channel below:
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?
i didn’t use any channels, the @spawn macro automatically creates the adequate channel
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
Thank you for your comment!!. Using future is a good solution.
Actually I can do same asynchronous computing with coroutine using this code.
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…
1 Like
Oh I found my mistake… I have to use put!, not push! in the fun function.
This code works.
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()