Hi experts I can read in this <kt.academy> article...
# getting-started
t
Hi experts I can read in this kt.academy article that
Copy code
Fan-out
Multiple coroutines can receive from a single channel; however, to receive them properly we should use a for-loop (consumeEach is not safe to use from multiple coroutines)
I wonder if this statement regarding consumeEach is still accurate? In which sense it is not safe?
f
with
consumeEach
, when the coroutine completes it will close the channel. That would affect the other coroutines that may still want to continue consuming from the channel, so I would think that's the reason.
🙏 1
t
Thanks for the answer Francesc. I'd expect the producer to close the Channel, not the consumer. What would be the functional way to consume messages from a Channel the same way a for-loop does?
f
a for loop is just the correct way if you have multiple consumers;
consumeEach
will do the clean-up when the consumer is done, but if you have multiple consumers you could launch them in the same scope and, when you're done with the processing, you can cancel the scope and release all the resources there. Not sure if this answers the question.
t
I believe it does. Thanks for the time!
👍 1