I have a Task
executed with @async
. This task writes to a Channel
multiple times (it is not known exactly how often), then reads from another Channel
once. This should repeat until some condition is met. The main thread should wait for all output of the task, then write to it once. This should repeat until the task is finished. What is sent to the task depends on what the task sent before.
Here is a contrived example:
function f(input, output)
while true
put!(output, rand(-1:1))
if rand(1:10) == 1
take!(input) == 0 && return
end
end
end
input = Channel{Int}(Inf)
output = Channel{Int}(Inf)
task = @async f(input, output)
while !istaskdone(task)
s = 0
sleep(0.01) # FIXME: wait until task is trying to take! from input
while isready(output)
s += take!(output)
end
println(s)
put!(input, s)
yield()
end
In this situation, how can I reliably tell if task is blocked or is still sending input? I’d be happy about any hints or suggestions.
(Note: I asked this question a few days ago on on reddit and didn’t get a solution. Reddit user u/wherrera10 suggested using an unbuffered Channel and isready
, but this only works to tell if a task is blocked trying to put!
to an unbuffered Channel
)