January 2019
Intermediate to advanced
520 pages
14h 32m
English
The tokio-threadpool crate contains a blocking function that's declared as follows:
pub fn blocking<F, T>(f: F) -> Poll<T, BlockingError> where F: FnOnce() -> T
This function expects any function that performs blocking operations and runs it in a separate thread, providing a Poll result that can be used by a reactor. It is a slightly low-level approach but it's actively used by tokio and other crates (to perform IO operations on files).
The positive side of this approach is that we don't need to create a thread pool manually. We can use the simple main function, as we've done before:
fn main() { let addr = ([127, 0, 0, 1], 8080).into(); let builder = Server::bind(&addr); let server = builder.serve(|| service_fn(|req| ...Read now
Unlock full access