Single-threaded tokio runtime

Everyone knows that tokio runtime can be setup to run on the currently running thread by using #[tokio::main(flavor = "current_thread").

However, it does spawns two more threads. Resulting the application runs on three threads.

How do I force tokio to use currently running thread (i.e main thread) and not to spawn additional threads?

I know the other threads are used for blocking operations, such as file I/O. Still, if it's possible for me to disallow tokio to spawn additional threads, let me know.

Thanks.

1 Like

Here's an earlier thread on this topic, although it seems there may not be a way to have exactly one thread.

1 Like

The current-thread runtime does not automatically spawn two additional threads. The threads you're talking about have to do with the spawn_blocking threadpool, but threads there are spawned on demand, and exit when not used. To avoid these threads, don't use operations that require them.

There isn't any way to disable the spawn_blocking threadpool. If you could, it would break things like tokio::fs entirely.

3 Likes

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.