Is it okay to start multiple tokio runtimes for different parts of the app?

I am building an app which uses multiple libraries which starts tokio runtimes. What I am doing at the moment is to initialize them in different threads Is this approach fine or should I consider doing something else ?

2 Likes

Maybe you don't need to create runtimes? There's tokio::runtime::current() that in async functions gives a handle to runtime that is running the async function. You can use that for spawning tasks and pass that to non-async functions.

Each runtime will have its own thread pool and some overhead. It's not too bad if you have only a few of them. Sometimes that's even desirable if you don't want one part of the app to monopolize the runtime and starve another part.

1 Like

I am using r2d2 postgres which creates its own runtime. In another post alice mentioned using bb8 but I prefer to use the blocking versions of the things. So I have 2 threads at this point starting actix tokio runtime and postgres tokio runtime

It's not ideal, but it isn't the end of the world. Did the crate I suggested in the other thread no do the trick?

1 Like

It does the trick but with my current design based on traits it doesnt really play nice however I will be taking your advice on that and instead of using traits I will be hiding impl details behind an opaque type.

Working with a concrete type I think I shouldnt have problems having async methods

Thanks a lot.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.