Can I use Dispatchers.Default for CPU bound/intensive operations?

Viewed 334

I wanted to understand best practices/approaches for performing blocking CPU operations in production grade server side applications. (Note that I am talking about server side and not Android apps)

What I mean by blocking CPU operation?

Operation which runs on and consumes CPU, for example, huge matrix multiplication, huge data transformation done in while loop etc.

My Setup

  • We are building kotlin dsl powered by coroutines
  • We have created our own CoroutineScope with single thread for non blocking operations so that mutating variables from callback are thread safe because all of them runs on same thread
  • We are recommending users to use withContext(Dispatchers.IO) for doing all the blocking IO operations

Open Questions

  • We have fixed strategy to do non blocking ops and thread safe mutations as well as doing IO ops as mentioned above

  • We are exploring options to perform blocking CPU bound operations as mentioned in the beginning

Some of the Solutions

  • Use Dispatchers.Default for do blocking cpu operations

    • Does anyone foresee any issue with using Default dispatcher?
    • What if other part of code/transitive libs are also using Default dispatcher?
  • Create separate CoroutineScope with fixed threads (usually equals to no. CPU threads) and then ask our users to use that isolated scope for running any blocking CPU bound operations

Which approach we should go with? pros and cons? Is there any other alternative good approach?

Side Note

Coming from Scala, it is usually not preferred to use Global execution context and Dispatchers.Default somehow maps to global context

0 Answers
Related