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

Andrew Gazelka

04/16/2018, 9:02 PM
I am trying to use coroutines with connections. However,
Jsoup
connections uses blocking (
Thread.sleep()
I think)... this effectively makes each coroutine the same as a thread. Is there anyway to turn these API `sleep()`s into `delay()`s?
g

gildor

04/17/2018, 1:15 AM
No, it’s impossible, you cannot magically convert blocking calls to non-blocking. The only way is wrap blocking API with own coroutine that uses custom coroutines dispatcher (to avoid blocking of CommonPool dispatcher) Or use async API/library. Why does Jsoup use Thread.sleep()? Maybe to avoid blocking just do not use Jsoup API that reads document from network and do that using some async http client. Or, after all just, wrap those sync calls to coroutine with own thread pool
19 Views