Providing futures with a CPU pool and waiting for them
Futures are usually assigned to a Task
, which gets assigned to an Executor
. When a task is awake, the executor will place the task into a queue, and will call poll()
on the task until the process has been completed. Futures offer us a few convenient ways to execute tasks:
- Spawn a future task manually with
futures::executor::block_on()
. - Using
futures::executor::LocalPool
, which is useful for performing many small tasks on a single thread. In our future returns, we would not be required to implementSend
since we are only involving the task on a single thread. However, you are required to usefutures::executor::spawn_local()
on theExecutor
if you omit theSend
trait. - Using
futures::executor::ThreadPool
, which allows us to offload tasks to other threads.
How to do it...
- Create a Rust project to work on during this chapter with
cargo new futures
. - Navigate into the newly-created
futures
folder. For the rest of this chapter, we will assume that...