pardom
03/19/2019, 5:49 PMquver
03/19/2019, 8:34 PMJob
was added to the coroutineContext
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
?groostav
03/19/2019, 9:38 PMChannel
a monad?JoakimForslund
03/20/2019, 10:30 AMarnaud.giuliani
03/20/2019, 3:08 PMstructured concurrency
. If I understand, the thing is to ensure that we reuse a proper CoroutineScope
instead of launching coroutines in the `Global`f scope. Then here is my question. I have 2 components: A
& B
. My component A
initiate a coroutine scope and I can run functions against that. How do I share/propagate it to my component B
on which I want to call functions.
class A {
val coroutineContext: CoroutineContext ...
}
class B {
fun myFunction(){
// run with A.coroutineContext?
}
}
Do we have to pass the coroutineContext
as an argument to any call of B
?kevinherron
03/20/2019, 6:37 PMsuspend
functions / coroutines not working?Marc Knaup
03/21/2019, 4:17 AM.produce
immediately starts its block
even if there is no subscriber for the ReceiveChannel
yet?
If so, how can I avoid that?ghedeon
03/21/2019, 7:56 AMDeffered
into another Deferred
with a custom exception? There is no coroutine context in that place, so I can't use .await()
joelpedraza
03/21/2019, 2:09 PMfun <A, B> Deferred<Pair<A, B>>.split(): Pair<Deferred<A>, Deferred<B>>
Is this possible?Marc Knaup
03/21/2019, 3:35 PMbreak
at some point?louiscad
03/21/2019, 5:25 PMwhile (true)
loops because while (isActive)
continues execution and doesn't throw:
@UseExperimental(InternalCoroutinesApi::class)
fun CoroutineContext.checkIsActiveOrCancelNow() {
val job = get(Job)
if (job != null && !job.isActive) throw job.getCancellationException()
}
This is quite useful in loops where I implement retrying.
The alternative to that coroutineContext.checkIsActiveOrCancelNow()
call is to use if (isActive.not()) yield()
, but I find that it doesn't communicate the intent well.
Do you think this should be integrated into kotlinx.coroutines? I'm personally inclined to open an issue there, but I want to know if I'm not alone first.spierce7
03/22/2019, 1:44 AMasad.awadia
03/22/2019, 2:25 AMcurioustechizen
03/22/2019, 12:45 PMsuspendCancellableCoroutine
. We're looking for ways to signal progress in addition to completion and error.
Currently, we pass a channel as a parameter to the suspending function through which we signal progress (this is conceptually similar to having a callback for progress). Are there other options? Like using the continuation object to signal progress?Icaro Temponi
03/22/2019, 4:37 PMasad.awadia
03/22/2019, 11:28 PMasad.awadia
03/23/2019, 5:18 AMimport io.javalin.Javalin
fun main(args: Array<String>) {
val app = Javalin.create().start(7000)
app.get("/") { ctx -> ctx.result("Hello World") }
}
is it possible somehow to add coroutine support to this i.e. each request gets launched in its own coroutine and allows me to have controllers which are composed of suspending functions - maybe allow me to return a response on the context object from a launched coroutine?ribesg
03/25/2019, 10:14 AMsequence { }
Satyam Agarwal
03/26/2019, 7:43 AMScheduledExecutorService
with coroutines, and I am not getting anywhere.
I have a scheduled task that does not return anything. So I somehow want to use launch
with the ScheduledExecutorService
. Can anyone please helpPaul Woitaschek
03/26/2019, 8:36 AMtjohnn
03/26/2019, 11:00 AMfun main() = runBlocking {
//sampleStart
launch {
repeat(1000) { i ->
println("I'm sleeping $i ...")
delay(500L)
}
}
delay(1300L) // just quit after delay
//sampleEnd
}
Prints 3 lines and exits because it uses a daemon thread
I'm sleeping 0 ...
I'm sleeping 1 ...
I'm sleeping 2 ...
But main waits for coroutineScope
to finish execution
fun main() = runBlocking { // this: CoroutineScope
launch {
delay(200L)
println("Task from runBlocking")
}
coroutineScope { // Creates a new coroutine scope
launch {
delay(900L)
println("Task from nested launch")
}
delay(100L)
println("Task from coroutine scope") // This line will be printed before nested launch
}
println("Coroutine scope is over") // This line is not printed until nested launch completes
}
And prints this
Task from coroutine scope
Task from runBlocking
Task from nested launch
Coroutine scope is over
Is there a magic done by coroutineScope
that makes it block the main from finishing? Any help is appreciated.winchester044
03/26/2019, 4:32 PMVsevolod Tolstopyatov [JB]
03/26/2019, 5:57 PMkotlinx.coroutines
1.2.0-alpha is here!
This is an alpha version of the major release with a lot of improvements in debuggability, diagnostics and performance.
Changelog:
* Debug agent now merges real stacktraces with coroutine stacktraces, its API is cleaned up and debug agent is on its road to stabilization
* CoroutineTimeout
test rule for JUnit4 to simplify testing and debugging with coroutines
* Futures are now integrated with structured concurrency
* Dispatchers.Unconfined
, MainCoroutineDispatcher.immediate
, MainScope
and CoroutineScope.cancel
are promoted to stable API
* CompletableJob
is introduced, Job()
and SupervisorJob()
are now completable
* ensurePresent
and isPresent
extensions for ThreadLocal
* ensureActive
extensions for CoroutineContext
, CoroutineScope
and Job
* Operator invoke
on CoroutineDispatcher
...and a lot more, full changelog: https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.2.0-alphaandrea.santurbano
03/27/2019, 8:19 AMFuture
implementation (https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/clients/producer/internals/FutureRecordMetadata.java) as Deferred
. Could this be a solution?
fun <T> Future<T>.asDeferred(): Deferred<T> {
return GlobalScope.async(<http://Dispatchers.IO|Dispatchers.IO>) {
get()
}
}
@rocketraman suggested to follow also this solution: https://stackoverflow.com/questions/50793362/how-to-use-suspendcoroutine-to-turn-java-7-future-into-kotlin-suspending-functio#answer-52901712
Which of these two is the most correct?mohsenoid
03/27/2019, 9:47 AMinterface CallLogDataStore {
fun getCallLogs(): Single<Array<CallLogEntity>>
}
class CallLogDataStoreImpl @Inject internal constructor(
private val contentResolver: ContentResolver
) : CallLogDataStore {
override fun getCallLogs(): Single<Array<CallLogEntity>> = Single.fromCallable(::queryAllCallLogs)
we can change it to Deferred
from the source:
interface CallLogDataStore {
suspend fun getCallLogsAsync(): Deferred<Array<CallLogEntity>>
}
class CallLogDataStoreImpl @Inject internal constructor(
private val contentResolver: ContentResolver
) : CallLogDataStore {
override suspend fun getCallLogsAsync(): Deferred<Array<CallLogEntity>> =
suspendCoroutine { queryAllCallLogs() }
or just keep it simple and let the use-case decide to execute it using Coroutines or not,
interface CallLogDataStore {
fun getCallLogs(): Array<CallLogEntity>
}
class CallLogDataStoreImpl @Inject internal constructor(
private val contentResolver: ContentResolver
) : CallLogDataStore {
override fun getCallLogs(): Array<CallLogEntity> =
queryAllCallLogs()
interface CallLogRepository {
suspend fun getCallLogsAsync(): Deferred<Array<CallLog>>
}
class CallLogRepositoryImpl @Inject internal constructor(
private val dataStore: CallLogDataStore,
private val mapper: CallLogMapper
) : CallLogRepository {
override suspend fun getCallLogsAsync(): Deferred<Array<CallLog>> =
GlobalScope.async { dataStore.getCallLogs().map(mapper::mapFromEntity).toTypedArray() }
}
any suggestion?mingkangpan
03/27/2019, 2:24 PMI/System.out: RendezvousChannel@6d58005{ReceiveQueued,queueSize=4}
can anyone elaborate what queueSize
means?paulblessing
03/27/2019, 2:42 PMJob
.
interface ActiveIndicator {
val isActive: Boolean
fun ensureActive()
}
private class JobActiveIndicator(private val job: Job) : ActiveIndicator {
override val isActive: Boolean get() = job.isActive
override fun ensureActive() {
job.ensureActive()
}
}
suspend fun doSomething() {
val job = coroutineContext[Job]!!
executeCancellableTask(JobActiveIndicator(job))
}
// (Possibly even in Java code somewhere)
fun executeCancellableTask(activeIndicator: ActiveIndicator) {
while (activeIndicator.isActive) {
// Do some work
}
}
mayojava
03/28/2019, 11:46 AMv0ldem0rt
03/28/2019, 2:50 PMExecutorService
to a coroutine dispatcher. How can I do it the other way around i.e. get ExecutorService from disptacherv0ldem0rt
03/28/2019, 3:11 PMCoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).async {
// Do some work
}
Would this link the scope that I just created to parent scope (i.e. would parent wait)v0ldem0rt
03/28/2019, 3:11 PMCoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).async {
// Do some work
}
Would this link the scope that I just created to parent scope (i.e. would parent wait)ahulyk
03/28/2019, 3:13 PMlaunch {
val data = async(IO) {
dataRepository.getData()
}
data.await()
}
streetsofboston
03/28/2019, 3:17 PMasync(context = <http://Dispatchers.IO|Dispatchers.IO>) {
// Do some work
}
v0ldem0rt
03/28/2019, 3:18 PMGlobalScope.async
. I am trying to make it behave like GlobalScope
gildor
03/28/2019, 3:33 PMv0ldem0rt
03/28/2019, 3:59 PM