tmg
05/26/2020, 11:16 AMchannel.send()
blocks, what’s the properway to do it?David Glasser
05/26/2020, 9:35 PMsuspend (I)->O
. I’d like to get the List<O>
out, running a bounded number of copies of the mapping function in parallel. I feel like this should be straightforward, eg with Flow, but I can’t quite get it to workTash
05/27/2020, 2:56 AMcallbackFlow
is it necessary to call close(exception)
if an operation inside it throws? i.e.
callbackFlow {
try {
val result = somethingThatThrows() // Try-catch or just let it be?
offer(result)
} catch (exception) {
close(exception)
}
awaitClose { ... }
}
Dsittel
05/27/2020, 6:05 AMflow.count() == 0 ?
kenkyee
05/27/2020, 11:57 AMSean Keane
05/27/2020, 12:41 PMinternal class MainScope<T>(private val coroutineContextProvider: CoroutineContextProvider, private val response: (Response<T>) -> Unit) : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = coroutineContextProvider.context() + job + exceptionHandler
private val job = SupervisorJob()
//This needs more investigation with thread scope
private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
logWarn(throwable.message.orEmpty())
response.invoke(Response(error = throwable, value = null))
}
}
When an exception occurs I want to capture it and pass it back through the response object.
class Response<out T>(val value:T?, val error: Throwable? = null) {
fun isSuccess(block: (T) -> Unit): Response<T> {
value?.let(block)
return this
}
fun isFailure(block: (Throwable) -> Unit): Response<T> {
error?.let(block)
return this
}
}
But the exception handler never gets called when a failure occurs.
This is how I launch.
MainScope(coroutineContextProvider, onResponse).launch {
//CODE
}
Am I missing something for handling the exception or should it just be captured when an exception occursSrSouza
05/27/2020, 4:21 PMjava.lang.VerifyError: Verifier rejected
using Android Minify (R8) and Coroutines 1.3.6? My current AGP version is 4.0.0-rc01
but I was getting the same issue in 3.6.3
Sam Garfinkel
05/28/2020, 3:39 PMFoo
, run Bar
on all of the `Foo`s in parallel, then rejoin the results of Bar(Foo)
as an iterable of Foo
in the original order they were given? Suppose Bar(Foo)
will not run the same amount of time per Foo
.nkiesel
05/28/2020, 7:56 PMfun readAll(): Stream<Metadata> {
return runBlocking {
channelFlow<Stream<Metadata>> {
Type.supportedTypes.forEach { type ->
launch(<http://Dispatchers.IO|Dispatchers.IO> + CoroutineName("Read all for $type")) {
send(metadataPersistenceHelper(type).readAll())
}
}
}.reduce { accumulator, value -> Stream.concat(accumulator, value) }
}
}
(Type
has multiple supported types and metadataPersistenceHelper
does the heavy IO). Does this makes sense or are we abusing coroutines here?taer
05/28/2020, 8:14 PMfun concat(first: Flow<Int>, second: Flow<Int>): Flow<Int> = flow {
first.collect { value ->
return@collect emit(value)
}
second.collect { value ->
return@collect emit(value)
}
}
What is the point of the return@collect
?Sam Garfinkel
05/28/2020, 8:45 PMEyeCon
05/29/2020, 12:53 AMQuy D X Nguyen
05/29/2020, 4:56 AMasync { suspendFunc() }.await()
different from suspendFunc()
by itself?Souhail Marghabi
05/29/2020, 5:40 AMLuis Munoz
05/29/2020, 7:18 AMSpent the last 2 hours trying to figure out why this won't work (won't print anything) when I have newFixedThreadPoolContext(1) but works if I have 2 or more threads. Can someone explain why that is. Maybe I am not using callbackFlow correctly?
@Test
fun `read range`() {
val ctx = newFixedThreadPoolContext( 1, "tThread") /// test only works if 2 or more
val scope = CoroutineScope(ctx)
runBlocking {
repeat(500) {
repo.save("test$it")
}
scope.launch {
repo.readRange().collect {
println(Belief.fromBytes(it))
}
}.join()
}
}
fun readRange(): Flow<ByteArray> {
return callbackFlow {
database.readAsync { tr ->
// reference: <https://apple.github.io/foundationdb/javadoc/com/apple/foundationdb/async/AsyncIterable.html>
// will interate all values in the database
val itr = tr.getRange(path.range()).iterator()
// hasNext() is blocking (will do network call and bring back a few items)
while (itr.hasNext()) {
sendBlocking(itr.next().value)
}
channel.close()
CompletableFuture.completedFuture(Unit)
}
awaitClose { }
} // end of callbackFlow
}
Lukas Lechner
05/29/2020, 2:18 PMLilly
05/29/2020, 2:55 PMmyanmarking
05/29/2020, 3:24 PMspierce7
05/29/2020, 4:09 PMGlobalScope
. When would it be bad to use GlovalScope
, and when would it be good?Pacane
05/29/2020, 4:14 PMkotlinx.coroutines.JobCancellationException: StandaloneCoroutine was cancelled;
Is there a way to know where this cancellation is coming from? I've put logging+breakpoints everywhere I manually cancel the job, but it's coming from somewhere else..?Rechee Jozil
05/29/2020, 8:50 PMCLOVIS
05/30/2020, 3:07 PMPromise
for JS, and an equivalent for other platforms?
I'm making a library to access a web service, and it doesn't feel right to add all of those functions by hand, I guess there's a better way?Gabriel Feo
05/30/2020, 7:02 PMd.medina
06/01/2020, 3:45 PMAlexStibbons
06/01/2020, 6:59 PMThomas
06/02/2020, 11:47 AMLukas Lechner
06/02/2020, 11:50 AMDominaezzz
06/02/2020, 4:44 PMkotlinx.coroutines
does Kotlin playground use? I'm trying to get this (https://pl.kotl.in/fkFVPgVP0) to work.Albert
06/02/2020, 5:53 PM.send
are done on a channel. At some point we close the channel, but we still have some .send
open. I assumed that .close
would throw an exception or stops suspending for the ones who tries to .send
. But this is not the case. So is it best practise with channel to drain them with .consume
or .consumeEach
?taer
06/02/2020, 10:58 PMrunBlocking
with great results. But, now I want to use a scope for the class. I have this:
private val scope = CoroutineScope(serverCoroutineDispatcher )
fun doWork() {
runBlocking(scope.coroutineContext) {
//.....
}
}
The runBlocking with the scope's context feels wrong. I could scope.launch
but then it would return instantly. What am I missing?taer
06/02/2020, 10:58 PMrunBlocking
with great results. But, now I want to use a scope for the class. I have this:
private val scope = CoroutineScope(serverCoroutineDispatcher )
fun doWork() {
runBlocking(scope.coroutineContext) {
//.....
}
}
The runBlocking with the scope's context feels wrong. I could scope.launch
but then it would return instantly. What am I missing?octylFractal
06/02/2020, 11:16 PMrunBlocking(ctx.newCoroutineContext(Job(ctx.coroutineContext[Job])))
basically, create a "child" of the contextrunBlocking
already creates a child coroutine, at least from static analysis. I'll test it outhenrikhorbovyi
06/02/2020, 11:21 PMscope.launch {
// your stuff
}
inside your doWork
function it will return instantly (as you noticed) because your function is not suspend or it is not waiting your launch to be completedrunBlocking(scope.coroutineContext) {
//.....
}
It causes an illusion that doWork is waiting for your process to be completed cause runBlocking blocks your current threadoctylFractal
06/02/2020, 11:28 PMrunBlocking
spawning a child job:
val ctx = CoroutineScope(Dispatchers.Default + SupervisorJob())
fun main() {
try {
runBlocking(ctx.coroutineContext) {
println("I am running...")
println("But now I will cancel the job by throwing! If I'm not a child coroutine, this _will_ cancel the ctx")
error("Test error")
}
} catch (e: Exception) {
println("The other runBlocking bailed out: ${e.message}")
}
runBlocking(ctx.coroutineContext) {
println("[A] I will never run if my Job is cancelled.")
}
// explicitly cancel now
ctx.coroutineContext.cancel()
runBlocking(ctx.coroutineContext) {
println("[B] I will never run if my Job is cancelled.")
}
}
results in
I am running...
But now I will cancel the job by throwing! If I'm not a child coroutine, this _will_ cancel the ctx
The other runBlocking bailed out: Test error
[A] I will never run if my Job is cancelled.
Exception in thread "main" kotlinx.coroutines.JobCancellationException: Job was cancelled; job=SupervisorJobImpl{Cancelled}@3e57cd70
Luis Munoz
06/03/2020, 2:48 AMsuspend fun disconnect(reason: DisconnectReason) {
withContext(coroutineContext){
agentConnection.send(Frame().setCommand(Frame.Command.DISCONNECT))
agentConnection.close(reason)
}
}
taer
06/03/2020, 2:25 PMCoroutineScope(serverCoroutineDispatcher)
it creates a Job as part of that if there isn't one already. Then when I runBlocking with that scope that then throws, shouldn't the Job be canceled in that case?