This hasn't got an effective answer. I'm getting t...
# coroutines
w
This hasn't got an effective answer. I'm getting the same problem. Anyone can help?
e
Can you elaborate a bit on your specific use-case, please. What exactly you are trying to achieve by waiting until the actor stops?
w
Copy code
private fun <T> asyncProduceConsume(
            consumerConcurrencyLimit: Int,
            producer: Producer<T>,
            consumer: Consumer<T>
    ) {
        runBlocking {
            actor<T> {
                arrayListOf<Job>().let { jobs ->
                    CoroutineSemaphore(consumerConcurrencyLimit).let { semaphore ->
                        for (product in this) {
                            launch {
                                semaphore.lock {
                                    consumer(product)
                                }
                            }.let { job ->
                                jobs.add(job)
                            }
                        }
                    }

                    for (job in jobs) {
                        job.join()
                    }
                }
            }.let { consumerActor ->
                async {
                    producer(consumerActor)
                }.also {
                    try {
                        it.await()
                    } finally {
                        consumerActor.close()
                        (consumerActor as Job).join()
                    }
                }
            }
        }
    }
I was trying to return the result only when the parent consumer actor finished consuming the products.