Hi everybody, is it possible to execute a blocking...
# announcements
l
Hi everybody, is it possible to execute a blocking IO operation (such as HTTP GET request) in a coroutine and make it suspend instead of blocking the thread?
k
#coroutines or google 🙂
l
the point is I tried it and it seems the coroutines are still blocking but not suspending ... so there is still only 1 coroutine per 1 thread - the only benefit is that a coroutine starts faster, but that's it
but thanks for the coroutines channel, I will give it a try there 🙂
k
Well to call blocking code you'll always need to "dedicate" a thread to that call.
l
hmh, so that means that basically if (probably) every IO operation in Java is blocking while waiting for an IO response, the coroutines can't do anything about that?
k
Yes. You can use a non-blocking library like
java.nio
I think.
l
hmh so maybe that could be the solution, I will check it, but otherwise it would have been quite pity 🙂
b
coroutines don't magically turn a blocking call into a non-blocking call, the blocking call will always block the thread it is on. you have to use a non-blocking version/api (like
java.nio
OR you can push the blocking call into a background thread so it doesn't block any other threads. The coroutine library makes this second option easy, since there are some blocking apis that can't be easily replaced
k
Well of course it is possible to do IO on coroutines, that's pretty much the point.
l
Thanks guys, I think that now I understand what is going on 🙂. I am going to look at the NIO Java lib just for the sake of exploration 🙂