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

coder82

09/04/2019, 8:47 AM
What happens if I use withContext(Dispatchers.IO) and execute a lot of calls, is there a limit? what if all the threads are busy spinning? How to provide backpressure? Don't want to reach out of memory. @gildor
Size of this thread pool can be configured
what if all the threads are busy spinning
Than coroutine will not be dispatched and will wait in the event queue nothing will be executed
c

coder82

09/04/2019, 8:52 AM
ok
g

gildor

09/04/2019, 8:53 AM
How to provide backpressure
There is no such mechanins on dispatcher level, task still will be added to event queue Tho, instead of run too many withConttext(IO) you can write own abstraction on top of it that limit amount of requests, for example use Semaphore or worker pool pattern
c

coder82

09/04/2019, 8:53 AM
but if the thread is not spinning, and suspended, then it's considered "free to be used", so 64 must be ok
for my use case
g

gildor

09/04/2019, 8:54 AM
thread cannot be suspended
c

coder82

09/04/2019, 8:54 AM
because all the threads will do is send an http request and parse some xml
g

gildor

09/04/2019, 8:54 AM
do is send an http request
Which can be improved by using asynchronous http client
c

coder82

09/04/2019, 8:55 AM
aha that's a good point
g

gildor

09/04/2019, 8:55 AM
or even blocking http client with asyncronous API which has own connection limit
c

coder82

09/04/2019, 8:55 AM
This is what _I am doing now
g

gildor

09/04/2019, 8:55 AM
parse some xml
Which sounds more like CPU-bound task, so you should probably use differentt dispatcher for this
c

coder82

09/04/2019, 8:55 AM
uhm true
rather Dispatchers.Default
g

gildor

09/04/2019, 8:56 AM
yes, or even create own dispatcher only for this task. depends on your case
<http://httpClient.post|httpClient.post>
is this Ktor Http Client?
c

coder82

09/04/2019, 8:58 AM
yes
it's the default one
g

gildor

09/04/2019, 8:58 AM
Ktor Http client already asyncronous (implementation depends on the used engine), so it anyway will not use and will not block your IO dispatcher
no need to use any dispattcher for tit
c

coder82

09/04/2019, 8:59 AM
I am using the Apache engine
g

gildor

09/04/2019, 8:59 AM
even in this case, everythting is asyncrnous
ktor client is asyncrnous, even if actual IO is blocking it’s already handled by own thread pool of the engine
c

coder82

09/04/2019, 9:00 AM
but the XML parsing is still expensive but maybe it's only that parth worth running on another thread
because yes the http request is already suspending and giving back the thread
g

gildor

09/04/2019, 9:01 AM
yes, probably, but only parsing
c

coder82

09/04/2019, 9:01 AM
good point
g

gildor

09/04/2019, 9:01 AM
also it would be much more clear from API point of view
c

coder82

09/04/2019, 9:02 AM
the xml i get can be huge that's why, I am afraid to stop for too long the caller thread
g

gildor

09/04/2019, 9:02 AM
Yes, this is why you should extract it to suspend function that use
withContext(SomeCPUBoundDispaccther)
, so it will be safe to use this function from any other suspend function
c

coder82

09/04/2019, 9:03 AM
👍
7 Views