question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IO thread pool for blocking calls

See original GitHub issue

We 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:closed
  • Created 6 years ago
  • Reactions:43
  • Comments:45 (33 by maintainers)

github_iconTop GitHub Comments

24reactions
elizarovcommented, Aug 5, 2018

The expected future looks like this. There going to be only a single pool of threads in addition to UI thread.

  • Experimental coroutines scheduler becomes 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.
  • Experimental coroutines scheduler backs IO dispatcher, too, but default parallelism of IO is effectively unbounded. You can do withContext(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.
  • You can create additional dispatchers backed by the same thread pool with a custom limit on parallelism for doing things like synchronous DB access (see #261).
5reactions
elizarovcommented, Sep 18, 2017

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:

launch(CommonPool) { ... cpu-bound code ... }
launch(IO) { ... blocking IO code ... }

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:

launch(DefaultDispatcher) { ... cpu-bound code ... }
launch(DefaultDispatcher) { blocking { ... blocking IO code ... } }

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:

launch { ... cpu-bound code ... }
launch { blocking { ... blocking IO code ... } }
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found