Tim Malseed
05/30/2021, 12:34 PMsuspend fun foo() {
collection.forEach { element ->
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
// Some long running task processing 'element'
}
}
}
I'm wondering what would be the simplest way to parallelize the long running tasks (each element would be processed in parallel)
I think this is what async
might be for? It's just not something I've used so far, and I'm not sure what the syntax is..diesieben07
05/30/2021, 12:37 PMsuspend fun foo(list: List<String>) {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
coroutineScope {
val futures = list.map {
async { it.uppercase() /* heavy computation here */ }
}
val results = futures.awaitAll()
}
}
}
Tim Malseed
05/30/2021, 12:41 PMsuspend fun foo(collection: List<Bar>) {
collection.map { element ->
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
async {
// Some long running task processing 'element'
}
}
}.awaitAll()
}
Might be sufficient (no need to use an additional scope?)diesieben07
05/30/2021, 12:51 PMwithContext
has coroutineScope as well.Tim Malseed
05/30/2021, 1:34 PMdiesieben07
05/30/2021, 1:40 PMTim Malseed
05/30/2021, 1:42 PMwithContext{}
inside the async (I'm trying to get back on the main thread to post messages to the UI)diesieben07
05/30/2021, 1:43 PMTim Malseed
05/30/2021, 1:44 PMsuspend fun foo(collection: List<Bar>) {
collection.map { element ->
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
async {
print("start of iteration ($element)")
doSomeStuff()
withContext(Dispatchers.Main) {
listener.notify()
}
doMoreStuff()
print("end of iteration ($element)")
}
}
}.awaitAll()
}
This prints something like
"start of iteration (element 1)"
"end of iteration (element 1)"
"start of iteration (element 2)"
"end of iteration (element 2)"withContext()
is a suspension point?diesieben07
05/30/2021, 1:49 PMasync(<http://Dispatchers.IO|Dispatchers.IO>)
insteadTim Malseed
05/30/2021, 1:51 PMsuspend fun foo(collection: List<Bar>) {
collection.map { element ->
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
async {
print("start of iteration ($element)")
doSomeStuff()
withContext(Dispatchers.Main) {
listener.notify()
}
doMoreStuff()
withContext(Dispatchers.Main) {
listener.notify()
}
doMoreStuff()
withContext(Dispatchers.Main) {
listener.notify()
}
print("end of iteration ($element)")
}
}
}.awaitAll()
}
async
instead of withContext()
, then when do I call await
on them?listener.notify()
needs to be called at the right timediesieben07
05/30/2021, 1:55 PMsuspend fun foo(collection: List<Bar>) {
coroutineScope {
collection.map { element ->
async(<http://Dispatchers.IO|Dispatchers.IO>) {
print("start of iteration ($element)")
doSomeStuff()
withContext(Dispatchers.Main) {
}
doMoreStuff()
withContext(Dispatchers.Main) {
}
doMoreStuff()
withContext(Dispatchers.Main) {
}
print("end of iteration ($element)")
}
}.awaitAll()
}
}
Tim Malseed
05/30/2021, 1:56 PMsuspend fun foo(collection: List<Bar>) {
collection.map { element ->
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
async {
Should be
suspend fun foo(collection: List<Bar>) {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
collection.map { element ->
async {
Zach Klippenstein (he/him) [MOD]
05/30/2021, 2:42 PMlaunch
instead of async
and join
instead of await
.Tim Malseed
05/30/2021, 2:43 PM