I have a system that until recently was using the threadpool create to have a maximum of 4 different threads running some tasks. This tasks are expensive (they read stuff from a database and process it, and the whole process can take up to 10 seconds), so that's why I was running them on a thread pool.
So far so good, the problem is that the function I need to call is now async, and I don't know how to do this in Tokio. It looks like there was a tokio thread_pool long ago, but it's now gone.
I cannot just tokio::spawn() this task, because they would all start hitting the database at the same time and they would start failing.
What would you suggest? Any directions/ideas? Thanks!
If your code is CPU-bound and you wish to limit the number of threads used to run it, you should use a separate thread pool dedicated to CPU bound tasks. For example, you could consider using the rayon library for CPU-bound tasks. It is also possible to create an extra Tokio runtime dedicated to CPU-bound tasks, but if you do this, you should be careful that the extra runtime runs only CPU-bound tasks, as IO-bound tasks on that runtime will behave poorly.
If you want to wait for a future to complete in a non-async function/thread, the block_on methods linked in my previous post could help, I think.
Yes, I see the problem now. A (counting) semaphore might be an alternative if you only want to limit the concurrent database access. (But not sure.)