Hello, I have a question. I want to perform a simp...
# coroutines
f
Hello, I have a question. I want to perform a simple background job using coroutines. Currently I have an
object
that does stuff and writes to a status object that I can then read to know the state of the job (like how many items are processed etc). This is obviously crap because I currently block the entire server. I now want to redo this the right way. I use
launch
since the job doesnt return anything, it just does stuff. I know I can ask that job
isActive()
and stuff like that but how can I ask access that jobs memory, like shared memory? I just want to ask how many items it has processed etc so I can show that in my frontend.
z
maybe use a Channel to send updates on progress? Like a Channel<Int> in your case?
from the docs: val channel = Channel<Int>() launch { for (x in 1..5) channel.send(x * x) channel.close() // we're done sending } // here we print received values using
for
loop (until the channel is closed) for (y in channel) println(y) println("Done!")
you could have your function return that channel after the launch, and have the caller then observe it
y
There are a few things here to unpack, firstly you can have a long running HTTP task that doesn't block the server, e.g. a delay() should free up the thread anyway so the server isn't blocked.
Is server, like client/server here? i.e. the client should show updates?
Maybe it's a good fit for websockets, to update the client?
f
Yeah it probably is. I didnt really bother with it though because this is not the official webpage, just some tooling for myself.