Hi! can someone pls help me if I am using `IOPool`...
# arrow
g
Hi! can someone pls help me if I am using
IOPool
in conjunction with
parTraverse
in the right way? I couldn't find any documentation about it.
Copy code
@Test
    fun `IODispatcher + parTraverse + blocking`() {
        val time = measureTimeMillis {
            runBlocking {
                evalOn(IODispatchers.IOPool) {
                    (1..20).parTraverse {
                        blockingWork(it)
                    }
                }
            }
        }
        println("Done in $time ms [${Thread.currentThread().name}]")
    }

 private fun blockingWork(i: Int) {
        Thread.sleep(1000)
        println("Work $i done [${Thread.currentThread().name}]")
    }
s
there should be a
parTraverse(IODispatchers.IOPool) { // effect here }
method
that way you can remove the
evalOn
g
Thanks @stojan
@stojan, how is using IOPool making parTraverse faster? What happens underneath? I compared these:
Copy code
@Test
    fun `parTraverse + blocking`() {
        val time = measureTimeMillis {
            runBlocking {
                (1..20).parTraverse {
                    blockingWork(it)
                }
            }
        }
        // 3s, multiple blocked bg threads
        println("Done in $time ms [${Thread.currentThread().name}]")
    }

@Test
    fun `IOPool + parTraverse + blocking`() {
        val time = measureTimeMillis {
            runBlocking {
                (1..20).parTraverse(IODispatchers.IOPool) {
                    blockingWork(it)
                }
            }
        }
        // 1s
        println("Done in $time ms [${Thread.currentThread().name}]")
    }
s
AFIK
IOPool
is backed by unbounded thread pool, so it's suitable for blocking work not sure what the default thread pool is (I would assume computation, which is backed by a thread pool with a cap determined by number of cores/processors)
g
Oh yes, I got confused seeing
ForkJoinPool-1-worker-15
on my 4 core machine for default dispatcher, but just realized there can be some hyperthreading on my core
s
not sure how the naming of threads is 🙂 but the main idea is: blocking IO goes on unbounded thread pool, computational work goes on a bounded one
g
I mean there were 16 threads on bounded pool
👍 1
0-15