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

projectmoon

06/05/2019, 1:29 PM
i am trying to wrap my head around scopes, still. most examples online are simplistic uses of
runBlocking
... use case here is that i need to, once a day run a bunch of operations as coroutines. currently this is a java scheduled executor, but i want to switch to coroutines for the actual running of the code.
i am thinking of using
runBlocking
and a bunch of
launch
calls inside of this
d

Dico

06/05/2019, 1:57 PM
runBlocking
scope has only 1 thread. Use a scope like this,
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
Then
scope.launch { /*worker code */}
p

projectmoon

06/05/2019, 2:24 PM
that looks about what i'm after, yes. the scope object is then used to join/wait/kill all the threads?
when scope dies, so does everything underneath
are there any built-ins of the corutine API for running a coroutine at a specific time? i know there is
timer
, but otherwise i guess i can use a scheduling library to actually fire up the workers
d

Dico

06/05/2019, 2:36 PM
You can use the
delay
function after starting the coroutine
p

projectmoon

06/05/2019, 2:37 PM
yes, couldu use that. although it's more like i have to schedule according to cron-esque policies
actually i can use delay + next execution time from spring scheduling, i think
d

Dico

06/05/2019, 3:53 PM
If you want things to happen at a particular time, you might be better off using cron jobs to start a process
p

projectmoon

06/05/2019, 5:02 PM
that is the most reliable approach, yes. right now i am just experimenting. but i want to in the end leverage a higher level cron facility
g

gildor

06/06/2019, 12:48 AM
Use scope like this
I don't see why scope is needed in this case, just GlobalScope is fine for non cancellable scope of some background tasks
d

Dico

06/06/2019, 2:24 AM
I wouldn't recommend using GlobalScope to someone who doesn't understand scopes
g

gildor

06/06/2019, 2:25 AM
I wouldn’t recommend to use
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
to someone who doesn’t understand scopes
it doesn’t add any value or safety, just more code
sure, you can cancel scope, but in this sample you also easily can cancel
Job
d

Dico

06/06/2019, 2:28 AM
It's just a decent example of what a scope can do and as should be evident, he immediately had a better idea of what scopes do
g

gildor

06/06/2019, 2:32 AM
Fair enough
p

projectmoon

06/06/2019, 10:30 AM
the proper solution is probably a supervisor scope, but right now i am just experimenting with converting a standard java scheduled executor service into something using coroutines
unfortunately all of this is backed by JPA at the moment, and the R2DBC library is not yet stable. so, i am not sure building coroutines on top will help that much
d

Dico

06/06/2019, 1:39 PM
You can use executor.asCoroutineDispatcher() instead of Dispatchers.Default
p

projectmoon

06/06/2019, 2:04 PM
that would be a good place to begin bridging
2 Views