You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there any reason why we can't iterate a RemoteChannel.
The documentation for Channel shows an example using a for x in channel loop, whereas the analogous example for RemoteChannel uses a while true loop. Presumably this is because iterate is not defined for RemoteChannel.
function do_work()
for job_id in jobs
exec_time = rand()
sleep(exec_time) # simulates elapsed time doing actual work
# typically performed externally.
put!(results, (job_id, exec_time))
end
end;
@everywhere function do_work(jobs, results) # define work function everywhere
while true
job_id = take!(jobs)
exec_time = rand()
sleep(exec_time) # simulates elapsed time doing actual work
put!(results, (job_id, exec_time, myid()))
end
end
Naively copying the iterate code from Channel to RemoteChannel appears to work for a simple case:
using Distributed
function Base.iterate(c::RemoteChannel, state=nothing)
try
return (take!(c), nothing)
catch e
if isa(e, InvalidStateException) && e.state==:closed
return nothing
else
rethrow(e)
end
end
end
Base.IteratorSize(::Type{<:RemoteChannel}) = Base.SizeUnknown()
c = Channel{Int}(10)
foreach(i->put!(c, i), 1:3)
close(c)
data = [i for i in c]
rc = RemoteChannel(() -> Channel{Int}(10))
foreach(i->put!(rc, i), 1:3)
close(rc)
data = [i for i in rc]
The text was updated successfully, but these errors were encountered:
Is there any reason why we can't iterate a
RemoteChannel
.The documentation for
Channel
shows an example using afor x in channel
loop, whereas the analogous example forRemoteChannel
uses awhile true
loop. Presumably this is becauseiterate
is not defined forRemoteChannel
.Channel
(https://docs.julialang.org/en/latest/manual/parallel-computing/#Channels-1)RemoteChannel
(https://docs.julialang.org/en/latest/manual/parallel-computing/#Channels-and-RemoteChannels-1)Naively copying the iterate code from
Channel
toRemoteChannel
appears to work for a simple case:The text was updated successfully, but these errors were encountered: