Scope question wrt channels and a "servlet style" ...
# coroutines
t
Scope question wrt channels and a "servlet style" setup.. We have a system that creates instance of our class(think servlet) and has lifecycles for that instance. In the "do get" method, I create a produce channel(Bar), and then proceed to consume from it. I might swap to flows, but in this case, is this all setup right wrt the structured concurrency? When I close the second instance, the channels seem to shutdown correctly. The only thing that feels like it's missing is a server level context(that would parent the
instanceScope
)
the
spawn
call in the system I'm converting is expected to block untill the work is done. Side question though. What
couroutingScope
is the
this
inside that runBlocking? The
Copy code
for (x in streamer)
call is a suspending call, so I'm curious what scope is managing that call
a
runBlocking with no context parameter will create a new top-level Job iirc
t
yeah.. I validated that just now in debugger.
a
It looks like you should do
runBlocking(instanceScope) { … }
uh,
runBlocking(instanceScope.context)
t
is that kosher w/ all the parent things? 🙂 I just read a bit in this channel about messing up the whole heirarchy sometimes.
a
although tbh I’d just have an
instanceJob : Job
and pass
context + instanceJob
as the parameter to
runBlocking
t
ah.. so my close() cancels the instanceJob instead
I see it both ways.. "dont manage jobs, that's the scope's job" vs this method
a
I think it’s all ok, a coroutine running with a top-level Job by accident is one of the pitfalls though
t
ah that CoroutineScope is just
context + Job()
a
so where is the Job getting created at all? I thought
CoroutineScope(..)
didn’t create one, as opposed to
coroutineScope { }
oh it does?
t
Copy code
ContextScope(if (context[Job] != null) context else context + Job())
The names are blurring now.. CoroutineScope, CoroutineContext.
It's at least workin. I'm gonna have to reread the hierarchy medium article 3 more times. 🙂
1
a
ah, it creates a Job only if there wasn’t one already
good luck!
t
thx!