https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

Jonathan Mew

06/03/2020, 2:46 PM
We have some HTTP requests which we are currently running consecutively but it's taking too long, so we would like to parallelise them. We would need to halt execution until they are all complete, and propagate an exception if any failed (wouldn't matter which exception was thrown if multiple requests fail). We don't need to capture any response data. What would be the neatest/most idiomatic way to achieve this?
(I suspect I could get something working, but reckon this domain has some useful gadgets that might be easy to miss...)
s

streetsofboston

06/03/2020, 2:53 PM
Copy code
allMyRequests.map { request -> async { request.invoke(input) } }.awaitAll()
j

Jonathan Mew

06/03/2020, 2:54 PM
Thank you! My initial stab was to correctly map to an async {} block, but wasn't aware of awaitAll() 👍
s

streetsofboston

06/03/2020, 2:55 PM
If you don’t have any results, use
launch
and
joinAll
j

Jonathan Mew

06/03/2020, 3:01 PM
would that still propagate an exception?
s

streetsofboston

06/03/2020, 3:03 PM
Yup. The top-most launch (
myScope.launch  { …. }
) will handle it and if you have a CoroutineExceptionHandler installed int that top-most scope, it will receive it.
j

Jonathan Mew

06/05/2020, 3:01 PM
So we gave the launch/joinAll a try and it ran fine but didn't speed things along after all. And I realise now that obviously it wouldn't, because we are doing blocking I/O 🤦 We are using the AWS Java SDK to interact with S3, and would prefer not to reimplement it. In that case, I guess we are back to using a classic thread pool? Or does Kotlin still offer something to field that side of things for us?
s

streetsofboston

06/05/2020, 3:04 PM
If you use Dispatchers.IO, you get to use a pool of about 64 threads (on the JVM) (it borrows from the Dipatchers.Default if it runs out of threads, iirc). If you need to control it your self, on the JVM, you can easily get a Dispatcher from a Java Executor that is configured with your own thread-pool management.
j

Jonathan Mew

06/05/2020, 3:19 PM
thank you!
3 Views