Andrea Giuliano
10/02/2021, 12:32 PMval results = endpoints.map {
async {
client.call(it)
}
}
results.awaitAll()
but of course that will wait for all the calls to come back, not just the first N.
I know I could solve this problem by using channels and subscribe n topics waiting for them, but I was wondering if there is a counterpart with async/await as well?
Also, as a side note, one thing I’ve noticed is that if the client is blocking (imagine a Thread.sleep) the async is called sequentially and I lose completely the parallelism..what am I doing wrong?ephemient
10/02/2021, 4:27 PMrunBlocking is a single threaded event loop. use another dispatcher to dispatch onto other threads (e.g. async(<http://Dispatchers.IO|Dispatchers.IO>) { ... }Andrea Giuliano
10/02/2021, 4:31 PMThread.sleep ?ephemient
10/02/2021, 4:33 PMdelay() or some other suspend funAndrea Giuliano
10/02/2021, 4:40 PMephemient
10/02/2021, 4:42 PMAndrea Giuliano
10/02/2021, 4:45 PMselect can do the trick on the “first n that completes” ?ephemient
10/02/2021, 4:46 PMThread.sleep in a suspend fun)ephemient
10/02/2021, 4:47 PMephemient
10/02/2021, 4:47 PMn timesephemient
10/02/2021, 4:48 PMn valuesAndrea Giuliano
10/02/2021, 4:49 PMephemient
10/02/2021, 4:49 PMAndrea Giuliano
10/02/2021, 4:54 PMephemient
10/02/2021, 4:55 PMval results = supervisorScope {
val channel = Channel()
try {
for (endpoint in endpoints) {
launch {
channel.send(client.call(endpoint))
}
}
List(n) {
channel.receive()
}
} finally {
cancelChildren()
}
}ephemient
10/02/2021, 4:55 PMAndrea Giuliano
10/02/2021, 4:56 PMAndrea Giuliano
10/02/2021, 4:57 PMlaunch block, the client is blocking, when you do cancelChildren they won’t be cancelled for real will they?ephemient
10/02/2021, 4:58 PMrunInterruptible {} they'll at least be sent a thread interrupt which should break out of well-behaved Java blocking methodsAndrea Giuliano
10/02/2021, 5:02 PMrunInterruptible block then, alrightephemient
10/02/2021, 5:03 PMephemient
10/02/2021, 5:04 PMAndrea Giuliano
10/02/2021, 5:05 PMAndrea Giuliano
10/02/2021, 5:05 PMcancelChildren() function, how can I get who’s still running in that example?ephemient
10/02/2021, 5:06 PMephemient
10/02/2021, 5:07 PMAndrea Giuliano
10/02/2021, 5:08 PMAndrea Giuliano
10/02/2021, 5:08 PMephemient
10/02/2021, 5:10 PMAndrea Giuliano
10/02/2021, 5:12 PMAndrea Giuliano
10/02/2021, 5:13 PM