`*receiveAsFlow*: This function allows the resulti...
# coroutines
u
*receiveAsFlow*: This function allows the resulting flow to have *multiple collectors*. However, each element emitted from the channel will only be delivered to *one* of the collectors (fan-out fashion).
which one? last? nondeterministic race? also whats fan out in this context? never heard it used to mean one of many
z
Easiest way to tell is look at the source. Probably each flow collector ultimately suspends on a receive call from the channel and the order in which those calls end up happening at runtime determines which gets the next element. Channels enqueue receivers and give the next element to the first receive call, but the order of those calls from flow is going to be effectively non deterministic.
u
so each collector is guaranteed to get different emits? uf..sounds like a footgun waiting to happen (im lookimg at nav3 results api in recipes)
z
sounds like a footgun waiting to happen
That's why it's called out in the docs
u
nevertheless the docs mention fan out, which sounds like a contradiction here, doesn’t it? how can one to one delivery be fan out
z
I think "fan out" in that sentence just means that all collectors may receive elements, as opposed to e.g. the first collector being guaranteed to steal the whole stream
👍 1
i
See the blog post on receiving events and the guideline of "Channels are used to handle events that must be processed exactly once": https://elizarov.medium.com/shared-flows-broadcast-channels-899b675e805c
thank you color 1
s
Hi @ursus I might be late to this conversation, however I was going through the discussion and found it interesting. I was also studying channels few days earlier. Not sure if my findings are related to this. If you run the below code: This has one producer with 2 consumers:
Copy code
import kotlinx.coroutines.launch
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel

fun main() {
    val scope = CoroutineScope(Dispatchers.Default)

    val numbers = Channel<Int>()

    val producer = scope.launch {
        numbers.send(1)
        numbers.send(2)
        numbers.send(3)
        numbers.send(4)
        numbers.close()
    }

    val consumer = scope.launch {
        for(num in numbers) {
            println("________ consumer1: ${num}")
        }
    }

    val consumer2 = scope.launch {
        for(num in numbers) {
            println("________ consumer2: ${num}")
        }
    }

    runBlocking {
        producer.join()
        consumer.join()
        consumer2.join()
    }
}
You'll get different outputs every time: 1st Run:
Copy code
________ consumer1: 1
________ consumer1: 2
________ consumer1: 3
________ consumer2: 4
2nd Run:
Copy code
________ consumer2: 2
________ consumer2: 3
________ consumer1: 1
________ consumer2: 4
3rd Run:
Copy code
________ consumer1: 1
________ consumer2: 3
________ consumer2: 4
________ consumer1: 2
Please try this: If there are any suggestions or inputs from anyone, please let me know.
u
yes 2 consumers are indeterministic and should be avoided
s
correct and thanks for the feeback 👍