How to check a RemoteChannel is open?


#1

You can check whether a RemoteChannel object isready, but isopen(::RemoteChannel) seems not to be defined.

Basically, I am using a RemoteChannel to transfer jobs to remote workers but these throw an error when the underlying channel gets closed.

Thanks!


#2

I wasn’t able to find anything about this in the documentation either.

However, I ended up with the following pattern.

addprocs(3)

println(nprocs())

arr = [i for i=1:4]

function producer(c::Channel)
    for item in arr
        put!(c, item)
    end
end

jobs = RemoteChannel(()->Channel(producer));
results = RemoteChannel(()->Channel(32));

@everywhere function do_work(jobs, results)
    while true
        x = try take!(jobs) end

        if x == nothing
            break
        end

        put!(results, (x, myid()))
    end
end

for p in workers() # start tasks on the workers to process requests in parallel
    remote_do(do_work, p, jobs, results)
end

results_taken = 0
while results_taken < length(arr)
    r = take!(results)

    results_taken += 1

    println(r)
end

close(results)