Created
March 11, 2019 07:32
-
-
Save nyinyithann/1393c931e1bc538a9256ae318c3c0f4f to your computer and use it in GitHub Desktop.
Async Download with ThreadPool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(result_map_or_else)] | |
extern crate num_cpus; | |
extern crate reqwest; | |
extern crate threadpool; | |
use std::error::Error; | |
use std::thread; | |
use threadpool::ThreadPool; | |
fn main() { | |
let urls = [ | |
"http://(wrong)www.google.com/", | |
"http://microsoft.com/", | |
"http://www.wordpress.com/", | |
"http://www.peta.org", | |
"https://www.rust-lang.org/", | |
"https://www.haskell.org/", | |
"https://fsharp.org/", | |
"https://(wrong)ocaml.org/", | |
"https://reasonml.github.io/", | |
"https://www.erlang.org/", | |
"https://elixir-lang.org/", | |
"https://github.com/alpaca-lang", | |
]; | |
urls.iter().for_each(|url| download_async(url)); | |
THREADPOOL.with(|pool| pool.join()); | |
} | |
thread_local!(static THREADPOOL: ThreadPool = ThreadPool::new(num_cpus::get())); | |
fn bind<F, G, T>(input: F, rest: G) | |
where | |
F: (Fn() -> T) + Sync + Send + 'static, | |
G: Fn(T) + Sync + Send + 'static, | |
{ | |
THREADPOOL.with(|pool| pool.execute(move || rest(input()))); | |
} | |
fn download_async(url: &'static str) { | |
bind( | |
move || { | |
print_msg("Downloading Url...", url); | |
reqwest::get(url).and_then(|mut r| r.text()) | |
}, | |
move |result| { | |
result.map_or_else( | |
|_| print_msg("Error occured!", url), | |
|html| { | |
print_msg("Displaying...", url); | |
println!("Total {:?}", html.len()); | |
}, | |
); | |
}, | |
); | |
} | |
fn print_msg(msg: &'static str, url: &'static str) { | |
println!("{:?}, Url = {}, {}", thread::current().id(), url, msg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment