Hexa
05/09/2019, 9:43 PMrunBlocking {
coroutineScope {
withTimeoutOrNull(10000L){ // wait time mili seconds
val a: Deferred<Unit> = async { s.deleteItem(xyz) }
val b: Deferred<Unit> = async { s.deleteItem(abc) }
launch {
a.await()
b.await()
}
}
}
}
My async calls returns Deferred<Unit> but I thought it might be better to explicitly call await() on each of them as this would force the tasks to go back in the main thread. I prefer to do it this way because I'm using AWS lambda and I read somewhere that the lambda can freeze/terminated if the tasks stays in the background thread, see https://stackoverflow.com/a/39668749louiscad
05/10/2019, 6:46 AMSlackbot
05/10/2019, 11:42 PMvoddan
05/11/2019, 5:07 PMlifecycleScope
cancellation.
What code did they have in mind? How does it work? Many thanks.
https://youtu.be/BOHK_w09pVA?t=1719▾
Sam
05/11/2019, 9:11 PM@RunWith(JUnit4::class)
class CoroutineTest {
@Test
fun `test coroutine cancel`() {
var backgroundWorkCanceled = false
runBlocking {
val scope = CoroutineScope( Dispatchers.Default + Job() )
scope.launch {
try {
backgroundWorkCanceled = false
delay(3000)
} catch( e : CancellationException ) {
backgroundWorkCanceled = true
}
}
delay( 2000 )
scope.cancel()
}
assert( backgroundWorkCanceled )
}
}
Viktor Qvarfordt
05/12/2019, 9:30 PMprojectmoon
05/13/2019, 10:45 AMflow {
while (true) { emit(x); delay(y);
}
Hexa
05/13/2019, 1:05 PMval deleteJob = runBlocking {
withTimeoutOrNull(10000L) {
launch(<http://Dispatchers.IO|Dispatchers.IO>) { s.deleteItem(xyz) }
launch(<http://Dispatchers.IO|Dispatchers.IO>) { s.deleteItem(abc) }
}
}
which one of these is the best way to handle a couroutine that timedout?
if(deleteJob!!.isCancelled){
//do something
}
// or
if(deleteJob == null){
//do something
}
dector
05/13/2019, 1:41 PMwithContext(IO)
is leaking scope through global queue after parent scope cancelation. Explicit withContext(coroutineContext + IO)
works as expected.
(Example in thread)Viktor Qvarfordt
05/13/2019, 11:06 PMribesg
05/14/2019, 9:24 AMexpect
runBlocking functions?rook
05/14/2019, 7:00 PMkrtko
05/15/2019, 4:50 PMFailed to start thread - pthread_create failed (EAGAIN) for attributes: stacksize: 512k, guardsize: 0k, detached.
Exception in thread "HTTP-listener-11490-1" java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached
at java.base/java.lang.Thread.start0(Native Method)
at java.base/java.lang.Thread.start(Thread.java:803)
at java.base/java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:937)
at java.base/java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:1005)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
It definitely wasn't running out of memory. We do spawn a lot of coroutines, however my understanding is that shouldn't be an issue. The one place we use coroutines is:
val pushNotificationService = this.pushNotificationService
GlobalScope.launch {
pushNotificationService.sendPushKit(id)
}
Any ideas what could be going wrong? ThanksKulwinder Singh
05/16/2019, 11:09 AMcontinuation.isCompleted
i have used this and its working fine, is it ok ? suspend fun <T> Task<T>.await(): T {
return suspendCancellableCoroutine { continuation ->
addOnSuccessListener {
if (continuation.isCompleted.not())
continuation.resume(it)
}.addOnFailureListener {
if (continuation.isCompleted.not())
continuation.resumeWithException(it)
}
}
}
Kulwinder Singh
05/16/2019, 11:23 AMRené
05/16/2019, 11:36 AMsuspend fun magic(token: String) {}
fun tokenMagic(refreshToken: String, work: (String)->Unit) =
runBlocking {
fetchToken(refreshToken)
}?.let(work::invoke)
fun example() {
tokenMagic("42") { token ->
magic(token) // magic should be only called from a coroutine
}
}
I would like to execute "work" within a runBlocking block, so that I doesn't need to add a runBlocking statment for each usage of tokenMagic
Albert
05/16/2019, 12:37 PMjoinAll
that it throws CancellationException
when this occurred in any of the launch {}
?rook
05/16/2019, 3:56 PMAlbert
05/16/2019, 5:06 PMCancellationException
does not cancel the parent Job
?Thomas
05/16/2019, 5:58 PMnewSingleThreadContext
should do what I want but that creates its own thread pool, which will cause unnecessary thread switching. The doc links to issue #261. Is there an update on that issue?Viktor Qvarfordt
05/16/2019, 7:53 PMcoroutineScope
and withContext(Dispatchers.Default)
? They both launch a suspend block with context Dispatchers.Default
. Is withContext
the same as coroutineScope { ... }.join()
? Is there a version of withContext
that defaults to use Dipsatchers.Default
similar to how coroutineScope
uses Dispatchers.Default
?Viktor Qvarfordt
05/16/2019, 9:00 PMmyList.map { myCpuIntensiveTask(it) }
?Viktor Qvarfordt
05/16/2019, 10:24 PMsuspend
, ie. suspend fun main() { ... }
? Is it equivalent to having fun main() = runBlocking { ... }
or something else?Joost de Vries
05/17/2019, 9:17 AMswitchMap
where showing search results while the user is typing. And I want to cancel all the previous requests to the server, just have the last one. In RxJs you use switchMap
for that. What's the coroutine equivalent?Paul Woitaschek
05/17/2019, 9:39 AMjava.lang.IllegalStateException: This job has not completed yet
?
kotlin
@Test
fun remoteConfigWorks() = runBlockingTest {
val remoteConfig = FirebaseRemoteConfig.getInstance()
val worked = suspendCoroutine<Boolean> { cont ->
remoteConfig.fetch().addOnCompleteListener {
if (it.isSuccessful) {
cont.resume(true)
} else {
cont.resumeWithException(it.exception ?: AssertionError("Fetching was not succssfull"))
}
}
}
assertThat(worked).isTrue()
}
Thomas
05/17/2019, 4:04 PMfun <T> Flow<T>.debounce(timeoutMillis: (T) -> Long): Flow<T>
pteale
05/18/2019, 3:58 PMpakoito
05/18/2019, 4:01 PMoverride fun onStart() {
super.onStart()
launch { contentSelectionPresenter.getAvailableContent() }
}
and whatever is within the launch block will be in a CommonPool thread and can be suspend funpteale
05/18/2019, 4:09 PMpteale
05/18/2019, 4:50 PMpteale
05/18/2019, 4:50 PMMatej Drobnič
05/18/2019, 4:50 PMpteale
05/18/2019, 4:51 PMMatej Drobnič
05/18/2019, 5:14 PMpteale
05/18/2019, 5:15 PM