Skip to content

Instantly share code, notes, and snippets.

@eagletmt
Created March 3, 2020 11:38
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 eagletmt/e0eddaaa93b30a29715549ffaeaacf8e to your computer and use it in GitHub Desktop.
Save eagletmt/e0eddaaa93b30a29715549ffaeaacf8e to your computer and use it in GitHub Desktop.
use rayon::prelude::*;
fn main() {
std::thread_local!(static CLIENT: std::cell::RefCell<Option<reqwest::Client>> = std::cell::RefCell::new(None));
rayon::ThreadPoolBuilder::new()
.num_threads(8)
.build_scoped(
|thread| {
CLIENT.with(|c| {
*c.borrow_mut() = Some(reqwest::Client::new());
});
thread.run()
},
|pool| {
pool.install(|| {
(0..20).into_par_iter().for_each(|_| {
CLIENT.with(|ref_client| {
let client = ref_client.borrow();
let client = client.as_ref().unwrap();
let mut rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let resp = client.get("https://google.com").send().await.unwrap();
let status = resp.status();
let body = resp.text().await.unwrap();
println!("{} {}", status, body.len());
});
});
});
})
},
)
.unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment