The Parallel Sweep
Stardate 55157.0 ยท UES Magnanimous
Briefing
Each sensor takes fifty milliseconds to answer. The full sweep reads them one after another, so eight sensors take four hundred milliseconds, and the Captain, who has been told the other ship is doing something, would like the number to be fifty.
The sensors do not depend on each other. Ask them all at once and wait for all the
answers. In Python that is asyncio.gather. In Rust it is a thread per
sensor, scoped so the compiler knows they finish before you return. The results must
come back in the order the sensors were asked, whatever order they answered in.
Mission objectives
ARCHIE checks all five every time you run diagnostics.
- โขReads every sensor and returns (name, value) pairs
- โขResults come back in the order asked
- โขAn empty list of sensors returns an empty result
- โขEight sensors finish in well under the sequential time
- โขValues are correct for each sensor
Objective
Rewrite sweep_all(names) so the sensor reads happen concurrently. Keep
read_sensor exactly as it is; it is the slow thing you are working around.
Return a list of (name, value) in the order given.
Press Run Diagnostics and I will tell you which objectives you actually met.
Objective
Rewrite sweep_all(names) so the sensor reads happen in parallel threads.
Keep read_sensor exactly as it is. Return (name, value) pairs
in the order given. std::thread::scope lets the threads borrow
names safely.
Press Run Diagnostics and I will tell you which objectives you actually met.
Ask Commander Raghunathan for a hint
Python: values = await asyncio.gather(*(read_sensor(n) for n in names)), then zip with the names. Rust: inside thread::scope, spawn one closure per name, collect the handles, then join them in order.
Debrief
Fifty-three milliseconds for the full sweep. The other ship is powering up its transporter. Commander Raghunathan has said, quietly, that she left something over there, and the Captain has asked what, and she has said "later".