Spawning to tokio threadpool while running on a localset

In my program's setup, I have a single-threaded event loop running part A of the program. The localset is .awaited inside an async function that itself is ran inside a threadpool. Part B runs in the threadpool. There are n=4 total threads in the threadpool.

Thus, when I .await a localset, does that imply that part B now has n=3 threads at its disposal?

Also: While inside the localset, I occasionally need to (safely) spawn a task onto the threadpool using tokio::task::spawn. When I do this, does it get spawned in one of the n=3 threads, or, is it ran in the n=1 localset thread?

Visualized better:

    /// This function is expected to be asynchronously executed from the context of the tokio runtime
    pub async fn execute(mut self) -> Result<(), NetworkError> {
        let local_set = tokio::task::LocalSet::new();
        let server = self.server.clone();
        let kernel = self.kernel;
        log::info!("Obtaining server remote ...");
        // The function in execute only spawns_local, but does not actually run anything until we run run_until with the local set
        let server_remote = HdpServer::load(server, &local_set).await.unwrap();
        log::info!("Done obtaining server remote ...");

        let _ = self.server_remote.replace(server_remote.clone());

        let server_to_kernel_rx = self.server_to_kernel_rx.take().unwrap();

        let server_remote_kernel = server_remote.clone();

        // Now, run the kernel in its own mutlithreaded environment,
        let _ = tokio::task::spawn(Self::multithreaded_kernel_inner_loop(kernel, server_to_kernel_rx, server_remote_kernel));
        // and run_until (single-thread) on the server. This starts all the futures loaded above
        local_set.await;
        log::info!("Kernel::execute is finishing ... program going to quit");
        Ok(())
    }

A LocalSet is not Send, so it cannot be awaited on the thread pool's own threads. You are probably awaiting it inside block_on, which has its own thread (the one it was called in), and doesn't run on the thread pool's own threads.

The tokio::spawn function spawns on the normal executor, so not inside the LocalSet.

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.