Tokio Tasks for CPU-bound ops

After examining the API, I can't really think of a reason why I'd want to use raw threads/Rayon for CPU-bound tasks over Tokio Tasks.

The latter gives us what we want for executing a set of CPU-bound calcs from which we need to retrieve the results, as long as the executor has enough threads to saturate available cores - and as long as we're not competing with a bunch of I/O bound stuff (although those tasks should be mostly blocking and not tying up threads, of course).

You run the risk of not allowing the I/O code to make progress, as discussed here:

If the executor is running nothing but CPU-bound tasks, it'll probably work OK (I did exactly this, before I knew better :slight_smile: ), but is still not recommended.

1 Like

This wouldn't be an issue in practice if your CPU calc were not hogging all the cores.

"Before you knew better"? What did you learn? :slight_smile:

Other languages/frameworks do provide concurrency systems that are suitable for shared CPU & IO-bound tasks - generally as long as you use some sort of "blocking" construct for the former (which often involves those being given to a dedicated thread pool).

Oh, "before I knew better" just means "before I read the docs"!

You can absolutely run CPU-bound tasks on tokio - just use spawn_blocking(). You can run a rayon thread pool from spawn_blocking(). But I think tokio and rayon both default to NUM_CPUs threads, which means they might squabble over the CPU. If your app is CPU-heavy, maybe set tokio to use just one or two threads, to handle the small amount of I/O.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.