Answer the question
In order to leave comments, you need to log in
How to parallelize correctly?
There is a function making 100 requests to the server. This takes approximately 5-10 seconds. (Although, there are times when you have to wait 16-18 seconds)
const PARALLEL_REQUESTS: usize = 8;
fn task(port: u64, start: u64) {
tokio::runtime::Runtime::new().unwrap().block_on(async move {
let client = reqwest::Client::builder()
.proxy(reqwest::Proxy::all(&format!("socks5h://127.0.0.1:{}", port)).unwrap())
.build().unwrap();
let mut offsets: Vec<u64> = vec![];
for i in (start..start + 1000).step_by(10) {
offsets.push(i);
}
let bodies = stream::iter(offsets)
.map(|offset| {
let client = &client;
let url = format!("https://site.ru/test?offset={}", offset).to_owned();
async move {
let resp = client.post(&url)
.header("X-Requested-With", "XMLHttpRequest")
.body("_ajax=1")
.send().await?;
resp.text().await
}
}).buffer_unordered(PARALLEL_REQUESTS);
bodies
.for_each(|b| {
async {
// getting some info
}
})
.await;
});
}
fn main() {
let start_time = Instant::now();
let n_workers = 4;
let mut tasks = vec![];
for i in 0..n_workers {
tasks.push(thread::spawn(move || {
task(9000 + i, 15 + (i * 1000));
}));
}
for task in tasks {
let _ = task.join();
}
let delta = Instant::now() - start_time;
println!("Time {}s", delta.as_secs_f64());
}
// Время выполнения функции task тоже измеряю
Takes 5.694900368s
Takes 9.479882614s
Takes 10.280974229s
Takes 12.753506558s
Takes 12.755521696s
Answer the question
In order to leave comments, you need to log in
With http requests, everything will rest on I / O, so it is very likely that the tor itself is slow - then it is better to regulate the number of simultaneous requests on the contrary.
I won’t tell you exactly how to do this, because I don’t understand rust very well.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question