本页尚未翻译,以下为英文原文。
#Concurrent downloader
本页内容
A pool of worker tasks shares out simulated resources (latency faked with task::sleep instead of a network), results come back over a channel, and a sorted report is printed.
#Code
nabi
// A "concurrent downloader": a pool of worker tasks fetches simulated resources.
enum FetchError {
NotFound,
Timeout,
}
enum Status {
Done(usize),
Failed(FetchError),
}
struct Outcome {
url: str;
status: Status;
}
const WORKERS: i32 = 3;
const DEADLINE_MS: i64 = 250;
// Pretend to download `url`: latency and size are derived from the name.
async fn fetch(url: str) throws (FetchError) -> usize {
let latency = if "slow" in url { 5000 } else { (url.len() * 7 % 40) as i64 };
await task::sleep(Duration::ms(latency));
if url.ends_with(".missing") { throw FetchError::NotFound; }
url.len() * 1024
}
// Fetch with a deadline; a fetch that takes too long is cancelled.
async fn fetch_with_deadline(url: str) -> Status {
let t = task::spawn(async {
Status::Done(try await fetch(url) catch e { return Status::Failed(e); })
});
try await task::timeout(Duration::ms(DEADLINE_MS), t) catch _ {
Status::Failed(FetchError::Timeout)
}
}
async fn main() {
let urls = [
"https://example.org/index.html", "https://example.org/logo.png",
"https://example.org/old.missing", "https://example.org/style.css",
"https://example.org/slow/video.mp4", "https://example.org/app.js",
"https://example.org/data.json", "https://example.org/about.html",
];
// A channel is multi-producer, multi-consumer: every worker holds a copy
// of one job receiver, and each URL goes to whichever worker takes it first.
let (job_tx, job_rx) = channel();
let (done_tx, done_rx) = channel();
for _ in 0..WORKERS {
let jobs = job_rx;
let done = done_tx.clone();
task::spawn(async {
loop {
let url = await jobs.recv_async() else { break; };
try! done.send(Outcome { url: url, status: await fetch_with_deadline(url) });
}
done.close();
});
}
done_tx.close();
// Queue every URL, then close the channel so idle workers stop.
for url in urls { try! job_tx.send(url); }
job_tx.close();
// Collect until every worker has closed its `done` sender.
let results: List<Outcome> = [];
for await r in done_rx { results.push(r); }
results.sort_by_key(|r| r.url);
let ok = 0;
let bytes = 0;
for r in results {
let name = r.url.strip_prefix("https://example.org/") ?? r.url;
match r.status {
Status::Done(n) => { ok += 1; bytes += n; println(f"{name:<16} {n:>7} B"); },
Status::Failed(FetchError::NotFound) => println(f"{name:<16} 404 not found"),
Status::Failed(FetchError::Timeout) => println(f"{name:<16} timed out"),
}
}
println(f"{ok}/{results.len()} downloaded, {bytes} bytes total");
}
about.html 30720 B
app.js 26624 B
data.json 29696 B
index.html 30720 B
logo.png 28672 B
old.missing 404 not found
slow/video.mp4 timed out
style.css 29696 B
6/8 downloaded, 176128 bytes total
#Walkthrough
- A channel has many senders and many receivers (MPMC). A copy of a
Receivershares its queue, so every worker takes a copy of the one job receiver, and each URL goes to whichever idle worker takes it first (2.17.4 multiple producers and consumers,channel). - Each worker gets its own sender via
clone()andclose()s it when done.for await r in done_rxends only once every sender is closed, somaincloses its originaldone_txearly (Concurrency). await jobs.recv_async()suspends the task, not the thread, until a value arrives. Oncemainclosesjob_txand the queue is empty it yieldsnone, and thebreakin thelet-elseleaves the worker loop.task::timeoutthrowsTimeoutErrorif the task does not finish in time, and cancels the task, so a slow fetch stops at its nextawait(task::racecancels the tasks that lose the same way) (task::timeout, 2.18.1 core API).- Results may arrive in a different order on every run, so they are sorted by URL before printing and the report is always the same.
Found a mistake? Edit this page on GitHub. Last updated