Using an executor
The second approach is to execute all tasks with an Executor instance. This allows you to run multiple tasks in a single thread, even if some tasks can't be completed immediately. To use an Executor, you have to create and run it, but it will block the current thread and you should add all the necessary tasks to be executed at the start.
For example, if you want to open a socket and process the stream of every incoming connection, you have to create a main Future that will read the Stream of incoming connections and spawn a handler for processing the Stream of the data of the connection using the tokio::spawn method. After you have created it, you have to spawn the whole processing future with the executor. Take a look ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access