Search code examples
rustconcurrencyparallel-processingrust-tokio

How to let tokio-rs know which Cores to use


In my current program I have to pin threads to handle cpu-intensive tasks, but I also use tokio-rs to take care of network I/O tasks.

Will tokio-rs try to use threads from the Cores I have allocated to dedicated tasks?

The goal would be, if the machine has 10CPUs, allocate 6 of them to intensive jobs, and 4 of them for I/O network jobs

I have taken a look at worker_threads, but I'm unsure if it tokio-rs will try to use the threads from the CPUs i have allocated to cpu-intensive tasks https://docs.rs/tokio/latest/tokio/runtime/struct.Builder.html#method.worker_threads


Solution

  • Let's assume you assign threads to CPU cores with the help of core_affinity crate. The good place to call its methods is the on_thread_start event handler. The provided callback will be run after a thread creation:

    let runtime = runtime::Builder::new_multi_thread()
        .worker_threads(4)
        .on_thread_start(|| {
            // call core_affinity methods
        })
        .build();
    

    If you create multiple runtimes, then no work-stealing happens between different runtimes.