IO thread pool for blocking calls
See original GitHub issueWe need some kind of IO
dispatcher in kotlinx.coroutines
that will be optimized for offloading of blocking calls and will be backed by an unbounded thread-pool that is smart enough to create new threads only when needed and to shut them down on timeout. The goal is that you could always wrap blocking IO call in withContext(IO) { ... }
(UPDATED: it was formerly named run(IO)
) and be sure that you will not run into the problem of having not enough threads in your pool. However, it will be up to the user to control the maximal number of concurrent operations via some other means.
It will be somewhat conceptually similar to newThread()
scheduler in Rx, but it will be also designed to be used where io()
scheduler in Rx is used.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:43
- Comments:45 (33 by maintainers)
Top Results From Across the Web
Java thread pooled server using blocking I/O - Stack Overflow
I want the thread pool to 'preempt' the blocking threads and use them to handle other sockets, like an operating system that suspends...
Read more >Best Practice: Should Not Block Threads - Monix
Blocking calls are error-prone because one has to be aware of exactly what ... If blocking, use a separate thread-pool for blocking I/O...
Read more >Making your program faster by relegating blocking I/O to ...
In that case, you can create (A) a pool of worker threads, and (B) a queue of messages to be sent, and have...
Read more >Blocking threads, suspending coroutines | by Roman Elizarov
The other way to block a thread is by using blocking IO (aka IO-bound task) to wait, for example, for a message from...
Read more >Questions about thread pool for blocking IO - Play Framework
Yes, you generally want to use a separate thread pool for blocking I/O. The right number of threads depends a lot on your...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The expected future looks like this. There going to be only a single pool of threads in addition to
UI
thread.DefaultDispatcher
by default (CommonPool' becomes deprecated). So, you
launch { … }` CPU-bound task in the default dispatcher. Its default parallelism is equal to the number of cores.IO
dispatcher, too, but default parallelism ofIO
is effectively unbounded. You can dowithContext(IO) { ... }
without having switch to a different thread, but if too many threads get blocked on IO, then new threads will spin up automatically to help.Let me clarify. Here is a problem we have: We need to be able to do both CPU-bound tasks and IO/blocking tasks with coroutines. The original proposal (this github issue) was to achieve this via a dedicated IO-tuned dispatcher, so you’d separate these two use-cases by choosing an appropriate dispatcher:
The alternative idea is to borrow Scala’s approach: instead of two separate dispatchers let’s have a single dispatcher (let’s call it
DefaultDispatcher
) and use it like this:What I particularly like about it, is that with this approach is makes sense to truly make this
DefaultDispatcher
a global default for all coroutine builders and just write: