Skip to content

Instantly share code, notes, and snippets.

@nyinyithann
Created March 11, 2019 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyinyithann/1393c931e1bc538a9256ae318c3c0f4f to your computer and use it in GitHub Desktop.
Save nyinyithann/1393c931e1bc538a9256ae318c3c0f4f to your computer and use it in GitHub Desktop.
Async Download with ThreadPool
#![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