docs

你正在阅读 v0.1.0 的文档。Nabi 0.1.0 已不再提供支持。 最新版本:v0.1.3 →

本页尚未翻译,以下为英文原文。

#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

#Walkthrough

  • A channel has many senders and many receivers (MPMC). A copy of a Receiver shares 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() and close()s it when done. for await r in done_rx ends only once every sender is closed, so main closes its original done_tx early (Concurrency).
  • await jobs.recv_async() suspends the task, not the thread, until a value arrives. Once main closes job_tx and the queue is empty it yields none, and the break in the let-else leaves the worker loop.
  • task::timeout throws TimeoutError if the task does not finish in time, and cancels the task, so a slow fetch stops at its next await (task::race cancels 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