Should clojure core.async channels be closed when not used anymore?
Asked Answered
W

1

11

Close method (at least in java world) is something that you as a good citizen have to call when you are done using related resource. Somehow I automatically started to apply the same for the close! function from core.async library. These channels are not tight to any IO as far as I understand and therefore I am not sure whether it is necessary to call close!. Is it ok to leave channels (local ones) for Garbage Collection without closing them?

Weissmann answered 5/3, 2015 at 21:43 Comment(0)
T
8

It's fine to leave them to the garbage collector unless closing them has meaning in your program. It's common to close them as a signal to other components that it's time to shut themselves down. Other channels are intended to be used forever and are never expected to be closed or garbage collected. In still other cases the channel is used to convey one value to a consumer and then both the producer and the consumer are finished and the channel is GC'd. In these last case there is no need to close them.

Trinetta answered 5/3, 2015 at 22:47 Comment(2)
Nice answer, just an example. "when has meaning" can be when you use async/reduce, which will need a closed input channel before yielding something.Allyce
the most common example is core.async/timeout which uses closing a channel to express that time has run out and no value was produced. typically closing a channel would happen after yielding a value or instead of one.Trinetta

© 2022 - 2024 — McMap. All rights reserved.