# Detecting a closed channel?

**URL:** <https://discourse.julialang.org/t/detecting-a-closed-channel/9289>\
**Category:** General Usage\
**Created:** [February 23, 2018, 11:02pm UTC](https://discourse.julialang.org/t/detecting-a-closed-channel/9289 "2018-02-23T23:02:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)\
**Post date:** [February 23, 2018, 11:02pm UTC](https://discourse.julialang.org/t/detecting-a-closed-channel/9289/1 "2018-02-23T23:02:57Z")

</div>

i have a very simple producer/consumer situation.

```julia
    chan = Channel{Array{Float64,1}}(1)
    f1() = use_data(chan, npoints)
    t = @task f1()
    schedule(t)
    yield()
    
    while true
        data = calc()
        put!(chan, data)
   end

```

When the task t has processed enough data, i.e. npoints of data, it closes the channel. I was assuming that it would be really easy for me to put something, e.g. isopen(chan), in the while loop to check for the closed channel and break.  
That doesn’t seem to work.  
What’s the best way to handle this ? Set up a 2nd channel and put! a value when the consumer is finishe ?

---

<div class="post-metadata">

**Author:** ![pasha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pasha/32/3319_2.png) [@pasha](https://discourse.julialang.org/u/pasha)\
**Post date:** [February 24, 2018, 2:20am UTC](https://discourse.julialang.org/t/detecting-a-closed-channel/9289/2 "2018-02-24T02:20:56Z")

</div>

I flailed around on a an approach resembling yours [in my MWE here](https://discourse.julialang.org/t/parallel-map-reduce-with-remotechannels-comments-on-mwe/8761) and hit the wall where there is no `isopen()` function nor an `open()` for `RemoteChannel`. In that MWE code above, I had to detect that the channel was closed by catching the error and exiting the loop. Crude, and there was no way to restart the thing because there is no `open()`

Later, I set up code to put zero-flagged jobs onto the job queue, and the workers would exit when they saw a zero job. There was a bit of flushing the pipes but it worked.

Then, I abandoned the whole approach and went to a variant of the scheduler [in the example code](https://docs.julialang.org/en/stable/manual/parallel-computing#Scheduling-1). This works better in my case, because `emit()` was causing too much data traffic, and it was better to accumulate map results in the worker before returning a result. I’m happy with this method.

So, the answer is that you have to catch the error, but there may be more elegant ways to do it.

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [January 19, 2019, 2:01pm UTC](https://discourse.julialang.org/t/detecting-a-closed-channel/9289/3 "2019-01-19T14:01:24Z")

</div>

Sorry to revive this old conversation, @pasha, but I am running into the same problems with `RemoteChannel` that you describe. I’m interested in the solution you ended up with, but the links in your paragraph about your variant of a scheduler no longer work, and I’m afraid I don’t understand the solution you ended up with. If you have time, could you please update the link or explain the scheduler in more detail?

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [January 19, 2019, 3:32pm UTC](https://discourse.julialang.org/t/detecting-a-closed-channel/9289/4 "2019-01-19T15:32:41Z")

</div>

I found the documentation that you linked to in the v0.6 docs. The example there was billed as a ‘simple pmap’, which led me to look through the `pmap` code and do some simple experiments. I hadn’t understood, until now, that `pmap` already feeds jobs to workers, as workers complete their jobs. For some reason I thought the jobs were split up evenly amongst workers.
