Created
March 3, 2020 11:38
-
-
Save eagletmt/e0eddaaa93b30a29715549ffaeaacf8e to your computer and use it in GitHub Desktop.
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
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